GDI+ 发生一般错误使用 SaveFileDialog 后在 Bitmap.Save() 处
我使用以下代码块,并在 using 块内添加更多代码:
using (System.Drawing.Bitmap tempImg =
(System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap))
{
// ...
tempImg.Save("..\\..\\testdata\\tempImg.bmp", ImageFormat.Bmp);
// ...
}
但我仍然收到错误:
GDI+ 发生一般错误
仅当我执行了一些与 using 块内的代码无关的操作后, 。在其他时候,这很有效。 tempImg.bmp 也是一个临时文件,因此我删除了 using 块本身中的 tempImg.bmp。
由于 tempImg 位于 using 内部并且已将其释放,因此我认为锁定问题应该得到解决。
有人可以告诉我这段代码有什么错误吗?
编辑: System.Drawing.Image
到 System.Drawing.Bitmap
作为 tempImg
的类型。
编辑: 我发现只有在创建 SaveFileDialog 并且用户单击“保存”后才会出现此错误。
I use the following code block with some more code inside the using block:
using (System.Drawing.Bitmap tempImg =
(System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap))
{
// ...
tempImg.Save("..\\..\\testdata\\tempImg.bmp", ImageFormat.Bmp);
// ...
}
But I still get the error:
A Generic Error occured at GDI+
only after I make some action which is not related to the code inside the using block. In other times this works well.
Also the tempImg.bmp is a temporary file, so I delete the tempImg.bmp within the using block itself.
Since the tempImg is inside the using and this it's disposed, I think the locking problem should be solved.
Can someone please let me know what is the mistake in this code?
Edit:System.Drawing.Image
to System.Drawing.Bitmap
as the type of tempImg
.
Edit:
I have identified I get this error only after SaveFileDialog is created and user clicks on 'Save'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
最后我可以找到我的代码中的错误,并想在这里提及它,因为我认为它可能对某人有用......
因为我在 tempImg.Save 中给出了相对路径,并且在用户单击“保存”后在SaveFileDialog中,tempImg.Save的实际路径变为:
SaveFileDialog指定的路径+自动相对路径
。
因此,如果该路径不存在,则会发生此错误。
谢谢大家的回答。
Finally I could find what was wrong in my code and would like to mention it here as I think it may be useful to someone....
As I have given a relative path in tempImg.Save, and after the user clicks 'Save' in SaveFileDialog, the actual path for tempImg.Save become :
Path specified by SaveFileDialog + the relative path
automatically.
Thus if the path does not exist, this error occurs.
Thanks every one for the answers.
我也曾经遇到过这个问题 - 它发生是因为位图锁定并且您无法保存它(如果您愿意我可以找到确切的解释)所以无论如何解决方法是这样的:
创建一个与原始位图大小相同的新位图 - 将原始位图复制到新创建的位图,然后处理原始位图并保存新位图。
然后保存。
I also had once this problem- it happens because the bitmap locks and you can't save it( if you want I can find the exact explanation) so anyway a fix around is this:
Create a new bitmap the size of the original bitmap you have- copy the original bitmap to the new created bitmap and then dispose the original bitmap and save the new one.
And then save.
这通常表明其他东西(可能是您自己的应用程序中的其他线程)已经在文件系统级别锁定了您尝试保存的目标文件。如果您查看内部异常,我相信它应该提到这一点。如果它不是直接在 InnerException 中,确认这一点(或发现它可能真正是什么)的另一种方法是在调试器中打开第一次机会异常,并观察“保存”下方抛出的异常,然后将其转换为此异常通用异常。
This is usually an indicator that something else, potentially some other thread in your own application, already has the target file that you're trying to save locked at the file system level. If you look at the inner exception I believe it should mention this. If it's not directly in the InnerException Another way to confirm this (or discover what it might really be instead) is to turn on first chance exceptions in the debugger and watch for what exception is being thrown "underneath" Save and then being turned into this generic exception.
尝试了这里给出的所有解决方案,但没有成功。最终找到了解决方案。
g.dispose();
System.IO.Directory.Exists(dir);
Tried all the solutions given here, but in vain. Found the solution eventually.
g.dispose();
System.IO.Directory.Exists(dir);
这是 ASP.NET 应用程序吗?
ASP.NET 中的 GDI+ 发生一般错误,主要是因为缺少目标文件夹/访问权限。
您的代码也可以简化为:
Is this an ASP.NET application?
A Generic Error occured at GDI+ in asp.net mostly because of missing target folder / access permissions.
Also your code could be simplified to :
就我而言,它是一个 ASP.NET 应用程序,我在其中替换了单个 DLL,并且只需在部署后重新启动应用程序池即可。然后效果很好。
In my case it was an ASP.NET application in which I replaced a single DLL, and I had to simply re-start the application pool after deployment. Then it worked fine.
这是来自 Microsoft 论坛的代码示例。
This is code sample from Microsoft Forums.
我试图从资源中保存图像,当我直接使用方法 Bitmap.Save(filepath) 时,它给了我太多 GDI 错误。
我认为我们可以通过克隆将以下相同的代码用于任何其他位图图像。
I am trying to save image from resource and it gives me too GDI error when I directly use the method Bitmap.Save(filepath).
I think We can use the same below code for any other bitmap image by cloning it.
保存图像后处理您的
bitMap
对象:Dispose your
bitMap
object after save image:就我而言,我将位图文件保存在与源相同的位置,
这就是问题所在。
我将位图保存到新位置,现在一切正常。
In my case, i was saving the bitmap file on the same location as the source,
So that's the problem.
I save the bitmap to the new location and all fine now.
我遇到了同样的问题,通过将图像类型“.bmp”更改为“.png”,它的工作形式是我
I was facing the same issue, by changing image type ".bmp" to ".png" its work form me