调整大小后无法删除或重命名原始文件 [英] Can't delete or rename original file after resizing
本文介绍了调整大小后无法删除或重命名原始文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此函数返回一个调整大小且居中的图像,我执行它调用一个URL thumb.aspx?image = test.jpg& width = 100& height = 50
问题是执行后,我无法重命名或从服务器删除原始文件。
This function return an resized and centered image, and I execute it calling an url thumb.aspx?image=test.jpg&width=100&height=50
the problem is that after the execution, I can't rename or delete the original file from server.
<%@ Import Namespace="System.Drawing" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
try {
Response.Cache.VaryByParams["Image;Width;Height;needToFill"] = true;
Response.ContentType = "image/jpeg";
System.Collections.Hashtable imageOutputFormatsTable = new System.Collections.Hashtable();
imageOutputFormatsTable.Add(System.Drawing.Imaging.ImageFormat.Gif.Guid, System.Drawing.Imaging.ImageFormat.Gif);
imageOutputFormatsTable.Add(System.Drawing.Imaging.ImageFormat.Jpeg.Guid, System.Drawing.Imaging.ImageFormat.Jpeg);
imageOutputFormatsTable.Add(System.Drawing.Imaging.ImageFormat.Bmp.Guid, System.Drawing.Imaging.ImageFormat.Gif);
imageOutputFormatsTable.Add(System.Drawing.Imaging.ImageFormat.Tiff.Guid, System.Drawing.Imaging.ImageFormat.Jpeg);
imageOutputFormatsTable.Add(System.Drawing.Imaging.ImageFormat.Png.Guid, System.Drawing.Imaging.ImageFormat.Jpeg);
string imageLocation = Server.MapPath(Request.QueryString["Image"]);
int Width = Convert.ToInt32(Request.QueryString["Width"]);
int Height = Convert.ToInt32(Request.QueryString["Height"]);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageLocation);
int sourceWidth = image.Width;
int sourceHeight = image.Height;
int sourceX = 0;
int sourceY = 0;
double destX = 0;
double destY = 0;
double nScale = 0;
double nScaleW = 0;
double nScaleH = 0;
bool needToFill=true;
nScaleW = ((double)Width / (double)sourceWidth);
nScaleH = ((double)Height / (double)sourceHeight);
if (Request.QueryString["needToFill"] != null) {
needToFill = Convert.ToBoolean(Request.QueryString["needToFill"]);
}
if (!needToFill) {
nScale = Math.Min(nScaleH, nScaleW);
} else {
nScale = Math.Max(nScaleH, nScaleW);
destY = (Height - sourceHeight * nScale) / 2;
destX = (Width - sourceWidth * nScale) / 2;
}
if (nScale > 1) nScale = 1;
int destWidth = (int)Math.Round(sourceWidth * nScale);
int destHeight = (int)Math.Round(sourceHeight * nScale);
System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(destWidth + (int)Math.Round(2 * destX), destHeight + (int)Math.Round(2 * destY));
bmPhoto.SetResolution(72, 72);
System.Drawing.Imaging.ImageFormat outputFormat = (System.Drawing.Imaging.ImageFormat)imageOutputFormatsTable[image.RawFormat.Guid];
ApplicationException ex= new ApplicationException(string.Format("destWidth:{0}, destX:{1}, destHeight:{2}, desxtY:{3}, Width:{4}, Height:{5}", destWidth, destX, destHeight, destY, Width, Height));
System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto);
Rectangle to = new System.Drawing.Rectangle((int)Math.Round(destX), (int)Math.Round(destY), destWidth, destHeight);
Rectangle from = new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
grPhoto.DrawImage(image, to, from, System.Drawing.GraphicsUnit.Pixel);
bmPhoto.Save(Response.OutputStream, outputFormat);
bmPhoto.Dispose();
grPhoto.Dispose();
}
catch (Exception ex){
System.IO.StreamWriter sw=null;
try{
sw=new System.IO.StreamWriter(Server.MapPath("error.txt"),true);
sw.WriteLine("Error : " + ex.Message + " processing " + Request.QueryString["Image"]);
}
catch{}
finally{sw.Close();}
Response.Redirect("thumberror.gif");
}
}
</script>
推荐答案
image
看起来并没有被处理掉。我建议使用using语句使资源管理更加清晰。
image
doesn't look like it's being disposed. I'd recommend using a using statement to make resource management more clear.
A 使用
语句允许在范围内放置一些东西,当范围留下时,它得到调用
调用它。例如:
A using
statement allows placing something in a scope, and when the scope is left, it gets Dispose
called on it. For example:
using(var image = new Bitmap(imageLocation))
{
//Use image here
} //image will be disposed here
这篇关于调整大小后无法删除或重命名原始文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文