用户登录
用户注册

分享至

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

  • 作者: 老司饥
  • 来源: 51数据库
  • 2022-10-20

问题描述

我使用以下代码块,并在 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.Image 到 System.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 中的Save"后,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.

谢谢大家的回答.

软件
前端设计
程序设计
Java相关