views:

37

answers:

2

I want it to only saves the resized images so have tried to do so it deletes the original image again after it has been uploaded but can not because it says the image is being used by another process please help.
and can not simply remove where it stores the original because it uses it to resize

i Use this code to save the fils

string tempPath = "Galleryt";
string imgPath = "Gallery";
string savePath =  Path.Combine(Request.PhysicalApplicationPath, tempPath);
string imgSavePath = Path.Combine(Request.PhysicalApplicationPath, imgPath);
string imgSavePath2 = Path.Combine(Request.PhysicalApplicationPath, imgPath);
string ProductImageNormal = Path.Combine(imgSavePath, imageName + Fileupload1.PostedFile.FileName);
string ProductImagetemp = Path.Combine(savePath, "t__" + imageName + Fileupload1.PostedFile.FileName);
string ProductImagetemp2 = Path.Combine(imgSavePath2, "b__" + imageName + Fileupload1.PostedFile.FileName);
string extension = Path.GetExtension(Fileupload1.PostedFile.FileName);

switch (extension.ToLower())
{
    case ".png": goto case "Upload";
    case ".gif": goto case "Upload";
    case ".jpg": goto case "Upload";
    case "Upload": Fileupload1.PostedFile.SaveAs(ProductImageNormal);
        ImageTools.GenerateThumbnail(ProductImageNormal, ProductImagetemp, 250, 350, true, "heigh");
        ImageTools.GenerateThumbnail(ProductImageNormal, ProductImagetemp2, 600, 600, true, "heigh");

        Label1.Text = "";
        break;
    default:
        Label1.Text = "Status: Denne filtype er ikke tilladt";
        return;

}

if i try to delete the original file just after with code

   File.Delete(Server.MapPath("~/Gallery/" + imageName + Fileupload1.PostedFile.FileName));

the method of GenerateThumbnail

  public static void GenerateThumbnail(string filePath, string OriginalFile, int width, int height, bool retainAspect, string quality)
{
    Bitmap bitmapNew;
    float fx, fy, f;
    int widthTh, heightTh;
    int widthOrig, heightOrig;
    bitmapNew = new Bitmap(filePath);

    if (retainAspect)
    {
        widthOrig = bitmapNew.Width;
        heightOrig = bitmapNew.Height;
        fx = widthOrig / width;
        fy = heightOrig / height;
        f = Math.Max(fx, fy);

        if (f<1)
        {
            f=1;
        }
        widthTh = (int)(widthOrig/f);
        heightTh = (int)(heightOrig/f);
    }
    else
    {
        widthTh = width;
        heightTh = height;


    }

    Size newSize = new Size(widthTh, heightTh);

    using(Bitmap thumb = new Bitmap((System.Drawing.Image)bitmapNew, newSize))
    {
        Graphics g = Graphics.FromImage(thumb);
        Int64 qualityLevel = 25L;

        if (quality == "high")
        {
             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
             qualityLevel = 95L;
        }
        if (quality == "medium" || quality == "low")
        {
             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            if (quality =="medium")
                qualityLevel = 65L;

        }
        System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
        System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
        eParams.Param[0]=new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityLevel);
        //Her genereres den nye thumbnail
        thumb.Save(OriginalFile, codec, eParams);
        thumb.Dispose();
    }
}
+1  A: 

If the ImageTools.GenerateThumbnail method can take a Stream, consider writing the PostedFile to a [MemoryStream][1] and passing that to this method before saving the thumbnails, or even using PostedFile.InputStream directly if possible. This way the original image is only ever in memory and you do not even save it to disk.

Oded
I agree, my guess is that the ImageTools class is using a Image i = Image.FromFile(...) and not calling i.Dispose() once finished.
Fredrik Johansson
can i turn off the function in a way that the release file ? thanks
saadan
@saadan - I don't understand.
Oded
it calls i.Dispose()
saadan
+1  A: 

The problem is located in code we cannot see, GenerateThumbnail(). Guessing at the way it might work, it creates a bitmap from ProductImageNormal. That puts a lock on the file which prevents it from being deleted. You'll have to call the bitmap's Dispose() method to release the lock.

Hans Passant
thanks you saved my life. bitmapNew.Dispose()
saadan