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

发布于 2024-11-03 18:33:08 字数 765 浏览 0 评论 0原文

我使用以下代码块,并在 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.ImageSystem.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

栩栩如生 2024-11-10 18:33:08

最后我可以找到我的代码中的错误,并想在这里提及它,因为我认为它可能对某人有用......

因为我在 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.

自由范儿 2024-11-10 18:33:08

我也曾经遇到过这个问题 - 它发生是因为位图锁定并且您无法保存它(如果您愿意我可以找到确切的解释)所以无论如何解决方法是这样的:
创建一个与原始位图大小相同的新位图 - 将原始位图复制到新创建的位图,然后处理原始位图并保存新位图。

Bitmap bm3 = new Bitmap(bm2);

然后保存。

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.

Bitmap bm3 = new Bitmap(bm2);

And then save.

我乃一代侩神 2024-11-10 18:33:08

这通常表明其他东西(可能是您自己的应用程序中的其他线程)已经在文件系统级别锁定了您尝试保存的目标文件。如果您查看内部异常,我相信它应该提到这一点。如果它不是直接在 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.

一生独一 2024-11-10 18:33:08

尝试了这里给出的所有解决方案,但没有成功。最终找到了解决方案。

  1. 处理应用于图像的任何图形:g.dispose();
  2. 确保保存路径存在:System.IO.Directory.Exists(dir);

Tried all the solutions given here, but in vain. Found the solution eventually.

  1. Dispose any Graphics applied on image: g.dispose();
  2. Make sure save path exists: System.IO.Directory.Exists(dir);
绾颜 2024-11-10 18:33:08

这是 ASP.NET 应用程序吗?

ASP.NET 中的 GDI+ 发生一般错误,主要是因为缺少目标文件夹/访问权限。

您的代码也可以简化为:

       using (Image image= dataObject.GetImage())
       {
            if (image != null)
            {
                image.Save("test.bmp");
            }
        }

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 :

       using (Image image= dataObject.GetImage())
       {
            if (image != null)
            {
                image.Save("test.bmp");
            }
        }
把梦留给海 2024-11-10 18:33:08

就我而言,它是一个 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.

又怨 2024-11-10 18:33:08

这是来自 Microsoft 论坛的代码示例。

// new image with transparent Alpha layer
using (var bitmap = new Bitmap(330, 18, PixelFormat.Format32bppArgb))
{
    using (var graphics = Graphics.FromImage(bitmap))
    {
        // add some anti-aliasing
        graphics.SmoothingMode = SmoothingMode.AntiAlias;

        using (var font = new Font("Arial", 14.0f, GraphicsUnit.Pixel))
        {
            using (var brush = new SolidBrush(Color.White))
            {
                // draw it
                graphics.DrawString(user.Email, font, brush, 0, 0);
            }
        }
    }

    // setup the response
    Response.Clear();
    Response.ContentType = "image/png";
    Response.BufferOutput = true;

    // write it to the output stream
    bitmap.Save(Response.OutputStream, ImageFormat.Png);
    Response.Flush();
}

This is code sample from Microsoft Forums.

// new image with transparent Alpha layer
using (var bitmap = new Bitmap(330, 18, PixelFormat.Format32bppArgb))
{
    using (var graphics = Graphics.FromImage(bitmap))
    {
        // add some anti-aliasing
        graphics.SmoothingMode = SmoothingMode.AntiAlias;

        using (var font = new Font("Arial", 14.0f, GraphicsUnit.Pixel))
        {
            using (var brush = new SolidBrush(Color.White))
            {
                // draw it
                graphics.DrawString(user.Email, font, brush, 0, 0);
            }
        }
    }

    // setup the response
    Response.Clear();
    Response.ContentType = "image/png";
    Response.BufferOutput = true;

    // write it to the output stream
    bitmap.Save(Response.OutputStream, ImageFormat.Png);
    Response.Flush();
}
埖埖迣鎅 2024-11-10 18:33:08

我试图从资源中保存图像,当我直接使用方法 Bitmap.Save(filepath) 时,它给了我太多 GDI 错误。
我认为我们可以通过克隆将以下相同的代码用于任何其他位图图像。

Private void SaveResourceImage() {
    object resBmpObject = Resource.Image1.Clone();//Bitmap Image from resource file
    //object resBmpObject = anyBmpImage.clone(); //for image other than resource image
    Bitmap resBmpImage = (Bitmap)resBmpObject;
    resBmpImage.Save(destFilePath, System.Drawing.Imaging.ImageFormat.Png);
    resBmpImage.dispose();
}

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.

Private void SaveResourceImage() {
    object resBmpObject = Resource.Image1.Clone();//Bitmap Image from resource file
    //object resBmpObject = anyBmpImage.clone(); //for image other than resource image
    Bitmap resBmpImage = (Bitmap)resBmpObject;
    resBmpImage.Save(destFilePath, System.Drawing.Imaging.ImageFormat.Png);
    resBmpImage.dispose();
}
想你的星星会说话 2024-11-10 18:33:08

保存图像后处理您的 bitMap 对象:

 bitMap.Dispose()
 oimg.Dispose()

 bitMap = Nothing
 oimg = Nothing

Dispose your bitMap object after save image:

 bitMap.Dispose()
 oimg.Dispose()

 bitMap = Nothing
 oimg = Nothing
沫雨熙 2024-11-10 18:33:08

就我而言,我将位图文件保存在与源相同的位置,
这就是问题所在。
我将位图保存到新位置,现在一切正常。

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.

独闯女儿国 2024-11-10 18:33:08

我遇到了同样的问题,通过将图像类型“.bmp”更改为“.png”,它的工作形式是我

I was facing the same issue, by changing image type ".bmp" to ".png" its work form me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文