在给予写入许可之后我也得到了错误“ GDI +中发生了一般性错误。 [英] after gave write permission also i am getting error " A generic error occurred in GDI+"

查看:68
本文介绍了在给予写入许可之后我也得到了错误“ GDI +中发生了一般性错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件保存在文件夹中。我给了正确的位置,并给了该特定文件夹的写入权限,但我仍然收到错误GDI +中出现一般错误。



这里我的代码片段:



conti,...



i am saving image in folder. i gave correct location and gave write permisstion for that particular folder, but still i am getting error " A generic error occured in GDI +".

here my code snippets:

conti,...

MemoryStream oStream = new MemoryStream();
bitmap.Save(oStream, System.Drawing.Imaging.ImageFormat.Png);

string location = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;  // till now no erro. 
bitmap.Save(location + "Image\\temp\\" + Session.SessionID + "\\" + PhotoID.Trim() + ".Jpeg", ImageFormat.Jpeg); // here i am getting that error.
Response.BinaryWrite(oStream.ToArray());
bitmap.Dispose();





如何解决这个问题。 Dispose();

推荐答案

我认为这与位图的创建方式有关。



基本上:

如果位图是从内存流创建的,则必须在Bitmap的生命周期内保持流打开。当你试图保存位图时,抛出GDI + Exception(在GDI +中发生一般错误。)



你没有显示如何创建位图但是从你描述的行为我几乎可以肯定这是它。







同时检查:



http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream [ ^ ]



和这个:



http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory- stream-is-closed [ ^ ]
I think it is related to how bitmap was created.

Basically:
If bitmap was created from memory stream, you must keep the stream open for the lifetime of the Bitmap. Otherwise GDI+ Exception is thrown ("A generic error occurred in GDI+".) when you try to save bitmap.

You don't show how bitmap is created but from behavior you describing I'm almost certain this is it.



Also check this:

http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream[^]

and this:

http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed[^]


当从文件构造Bitmap对象时,该文件在对象的生命周期内保持锁定状态。因此无法修改图像并将其保存回构建的同一文件。



尝试如下



When Bitmap object constructed from a file, the file remains locked for the lifetime of the object. So cannot modify image and save it back to the same file where it constructed.

Try like below

using (MemoryStream oStream = new MemoryStream())
{
    using (FileStream fs = new FileStream(location, FileMode.Create, FileAccess.ReadWrite))
    {
        bitmap.Save(oStream , ImageFormat.Png);
        byte[] bytes = oStream.ToArray();
        fs.Write(bytes, 0, bytes.Length);
    }
}





希望这可以帮助你...



Hope this helps you...


这篇关于在给予写入许可之后我也得到了错误“ GDI +中发生了一般性错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆