Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi , i open an image whit the below code :

C#
protected void Page_Load(object sender, EventArgs e)
{
    string imagePath = Server.MapPath(PublicClass.ImgPath()) + "3_nissan-cars-logo-emblem.jpg"; // address of my local image
    Image originalImage = Image.FromFile(imagePath);
    System.Drawing.Image image;
    image = ResizeImage(originalImage, 100, 100);
    Response.ContentType = "image/Jpeg";
    image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}


C#
public static System.Drawing.Image ResizeImage(System.Drawing.Image image, int maxWidth, int maxHeight)
{
    int newWidth = 0, newHeight = 0;


    if ((image.Width > maxWidth || image.Height > maxHeight) && maxWidth > 0 && maxHeight > 0)
    {
        newWidth = maxWidth;
        newHeight = image.Height * newWidth / image.Width;


        var newImage = new Bitmap(maxWidth, newHeight);
        using (var graphic = Graphics.FromImage(newImage))
        {
            graphic.DrawImage(image, 0, 0, newWidth, newHeight);
        }
        return newImage;
    }
    else
        if (maxWidth > 0 && maxHeight > 0)
        {
            var newImage = new Bitmap(image.Width, image.Height);
            using (var graphic = Graphics.FromImage(newImage))
            {
                graphic.DrawImage(image, 0, 0, image.Width, image.Height);
            }
            return newImage;
        }
        else
            return image;
}


and i would like to delete image from my directory while its open and i get error regarding "image is running in another proccessor"

please help me that how to show image whit my above code and discard its stream to can delete it . thank you .
Posted
Comments
[no name] 19-Oct-14 14:23pm    
Asking the same question over and over tends to annoy people. Use a using block to properly dispose of your object.

1 solution

A simple answer is that every stream and resource using code exposes this method .Close() by using which, you can easily close the resource so that a next process can easily use it. Without having to throw an exception to you.

I have already posted an answer a few days ago, which had information about closing the streams and how to use the .NET code in C# efficiently to close the streams automatically once you're done working with them.

How to close file stream[^]
 
Share this answer
 
Comments
Member 11125813 19-Oct-14 17:11pm    
Thank you for your answer . actually i didnt get my appropriate result from your answer then i try from another way and i still have the previos problem . if you see my currently code you can find out that i didnt use fileStream directly . i used image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); which i tried image.discard() but i got the same error . i just wanna know how can i delete my file . stream and other stuffs arent important for me .
thank you .
Afzaal Ahmad Zeeshan 20-Oct-14 10:23am    
There is a method called, .Dispose(), did you try that one?
Afzaal Ahmad Zeeshan 20-Oct-14 10:24am    
Secondly, if you continue recieving this error, try to Redirect the user to a new method where you will delete the file and inside this very function Close the stream usage.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900