使用 SaveFileDialog 后,在 Bitmap.Save() 处的 GDI+ 处发生一般错误

A Generic error occurs at GDI+ at Bitmap.Save() after using SaveFileDialog(使用 SaveFileDialog 后,在 Bitmap.Save() 处的 GDI+ 处发生一般错误)
本文介绍了使用 SaveFileDialog 后,在 Bitmap.Save() 处的 GDI+ 处发生一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 using 块中使用了以下代码块和更多代码:

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);                            
    // ...
}

但我仍然收到错误:

GDI+ 发生一般错误

A Generic Error occured at GDI+

只有在我做了一些与 using 块内的代码无关的操作之后.在其他时候,这很好用.tempImg.bmp 也是一个临时文件,所以我在 using 块本身中删除了 tempImg.bmp.

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.

由于 tempImg 在 using 中,并且已将其处理,因此我认为应该解决锁定问题.

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?

System.Drawing.ImageSystem.Drawing.Bitmap 作为 tempImg 的类型.

System.Drawing.Image to System.Drawing.Bitmap as the type of tempImg.

我已经确定只有在创建 SaveFileDialog 并且用户单击保存"后才会收到此错误.

I have identified I get this error only after SaveFileDialog is created and user clicks on 'Save'.

推荐答案

我终于找到了我的代码有什么问题,并想在这里提一下,因为我认为它可能对某人有用....

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....

由于我在 tempImg.Save 中给出了相对路径,并且用户在 SaveFileDialog 中单击保存"后,tempImg.Save 的实际路径变为:

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 :

SaveFileDialog指定的路径+相对路径

Path specified by SaveFileDialog + the relative path

自动.

因此如果路径不存在,就会出现这个错误.

Thus if the path does not exist, this error occurs.

感谢大家的回答.

这篇关于使用 SaveFileDialog 后,在 Bitmap.Save() 处的 GDI+ 处发生一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)