2015-06-06 38 views
1

我有一个在我的asp.net C#应用程序 旋转图像的代码,这是我的代码:System.Runtime.InteropServices.ExternalException:GDI中发生一般性错误+

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(字符串文件名,ImageCodecInfo编码器,EncoderParameters encoderParams)在System.Drawing.Image.Save(字符串文件名,格式的imageformat)在System.Drawing.Image.Save(字符串文件名)

有时出现以下错误

System.OutOfMemoryException:内存不足。在 System.Drawing.Image.FromFile(字符串文件名,布尔 useEmbeddedColorManagement)在System.Drawing.Image.FromFile(字符串 文件名)

我读到这个错误的所有文章和解决方案,并没有什么工作。有些人说:“确保所需的文件夹具有读/写权限” 。 它有权限..

有人说,“你必须处置图像对象释放服务器上的内存。” , 我已经有了代码img.Dispose();释放图像文件,

代码中可能存在什么问题?有什么建议?

+1

(http://stackoverflow.com/questions/10984336 [你try catch块的结尾使用最后] /净使用块的VS调用处置)或尝试[使用语句](http://stackoverflow.com/questions/2808753/right-way-to-dispose-image-bitmap-and-picturebox )。我很好奇,你读过什么文章? – lloyd

+1

我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

+0

所以你认为我必须把它放在里面使用?喜欢这个? .... 使用(IMG为System.Drawing.Image = System.Drawing.Image.FromFile(路径)) { //旋转图像 img.RotateFlip(RotateFlipType.Rotate180FlipXY); //将图像保存到文件中 img.Save(path); //发布图像文件 img.Dispose(); } – amal50

回答

0

我改变了这个(Rotate180FlipXY)至(Rotate180FlipNone)后,问题已经解决

相关问题