Would have only saved the resized images
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 cannot because it says the image is being used by another process. Please help.
Cannot simply remove where it stores the original because it uses it to resize.I use this code to save the files:
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 "U开发者_Python百科pload": 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();
}
}
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.
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.
精彩评论