【发布时间】:2015-08-21 02:44:29
【问题描述】:
我有一个在我的 asp.net C# 应用程序中旋转图像的代码 这是我的代码:
protected void ImgBtn180_Click(object sender, ImageClickEventArgs e)
{
try
{
string vImageName = LblFarmId.Text;
string vPath = "~/attachments/survey/" + vImageName + ".jpg";
Image1.ImageUrl = vPath;
//get the path to the image
string path = Server.MapPath(vPath);
//create an image object from the image in that path
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
//rotate the image
img.RotateFlip(RotateFlipType.Rotate180FlipXY);
//save the image out to the file
img.Save(path);
//release image file
img.Dispose();
Random rnd = new Random();
Image1.ImageUrl = vPath + "?" + rnd;
}
catch (Exception ee)
{
LblCatchError.Text = ee.ToString();
}
}
当我在服务器上运行它时,有时会出现以下错误
System.Runtime.InteropServices.ExternalException:GDI+ 中出现一般错误。在 System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at System.Drawing.Image.Save(String filename)
有时还会出现以下错误
System.OutOfMemoryException:内存不足。在 System.Drawing.Image.FromFile(字符串文件名,布尔值 在 System.Drawing.Image.FromFile(String 文件名)
我阅读了有关此错误的所有文章和解决方案,但没有任何效果。 有人说,“确保所需的文件夹具有读/写权限。” 它有权限..
有人说,“你必须配置图像对象来释放服务器上的内存。” , 我已经有了代码 img.Dispose();释放图像文件,
代码中可能存在什么问题?有什么建议吗?
【问题讨论】:
-
use a finally at the end of your try catch block 或尝试using statement。我很好奇,你读过哪些关于这方面的文章?
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
所以你认为我必须把它放在里面使用?像这样? .... using (System.Drawing.Image img = System.Drawing.Image.FromFile(path)) { //旋转图像 img.RotateFlip(RotateFlipType.Rotate180FlipXY); //将图像保存到文件 img.Save(path); //释放图像文件 img.Dispose(); }
-
这是针对类似问题的不良解决方案示例............ stackoverflow.com/questions/15571022/…
-
@lloyd Thx,我做了你让我做的事情,但我仍然有同样的错误
标签: c# asp.net .net image generics