只会保存调整大小的图像
我希望它只保存调整大小的图像,所以尝试这样做。它在上传后再次删除原始图像,但不能,因为它说该图像正在被另一个进程使用。请帮忙。
无法简单地删除存储原始文件的位置,因为它使用它来调整大小。
我使用此代码来保存文件:
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;
}
如果我尝试使用
File.Delete(Server.MapPath("~/Gallery/" + imageName + Fileupload1.PostedFile.FileName));
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();
}
}
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 "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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在我们看不到的代码中,GenerateThumbnail()。猜测它可能的工作方式,它从 ProductImageNormal 创建一个位图。这会对该文件加锁,以防止其被删除。您必须调用位图的 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.
如果
ImageTools.GenerateThumbnail
方法可以采用Stream
,请考虑将PostedFile
写入 [MemoryStream][1]
并在保存缩略图之前将其传递给此方法,甚至使用PostedFile.InputStream
。这样,原始图像仅存在于内存中,您甚至不需要将其保存到磁盘。If the
ImageTools.GenerateThumbnail
method can take aStream
, consider writing thePostedFile
to a [MemoryStream][1]
and passing that to this method before saving the thumbnails, or even usingPostedFile.InputStream
directly if possible. This way the original image is only ever in memory and you do not even save it to disk.