C#位图保存通用GDI +错误 [英] C# Bitmap Save Generic GDI+ Error

查看:93
本文介绍了C#位图保存通用GDI +错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码给了我一些问题:

This code is giving me problems:

        <br />
private void SaveBMP(Bitmap bmp)<br />
        {<br />
            bmp.Save(_project.MapFilePath);<br />
        }



此行抛出通用GDI +错误。

bmp.Save(_project.MapFilePath);



有什么想法吗?



引用:

这意味着文件路径指向已在GDI +中打开的图像。完成后,您需要小心关闭图像。网上有大量的信息,但这是你找到的所有信息的核心。 GDI +中的错误会导致文件打开,除非你在使用它们时非常小心。



如何关闭位图图像?


This line throws a generic GDI+ error.
bmp.Save(_project.MapFilePath);

Any ideas?

quote:
It means that the file path points to an image that has already been opened in GDI+. You need to be careful to close your images when you are done with them. There's tons of info on this on the web, but that's the core of all the info you'll find. Bugs in GDI+ cause it to hold a file open, unless you're very careful in how you use them.

How do I close a bitmap image?

推荐答案

这意味着文件路径指向已在GDI +中打开的图像。完成后,您需要小心关闭图像。网上有大量的信息,但这是你找到的所有信息的核心。 GDI +中的错误会导致文件打开,除非你在使用它们时非常小心。



你可以通过调用它的Dispose方法来关闭它。任何具有Dispose方法的东西,你应该在完成使用它时调用。我通常写一个帮助类来加载图像,制作副本,关闭原始文件并返回副本,这样我就不会遇到这个问题。
It means that the file path points to an image that has already been opened in GDI+. You need to be careful to close your images when you are done with them. There's tons of info on this on the web, but that's the core of all the info you'll find. Bugs in GDI+ cause it to hold a file open, unless you're very careful in how you use them.

You close it by calling it's Dispose method. Anything that has a Dispose method, you should call when you finish using it. I typically write a helper class that loads an image, makes a copy, closes the original and returns the copy, so that I never have this problem.


当然 - 这意味着那个图像是开放的,是你正在使用的那个,这并不奇怪。
Sure - that means that the image that was being held open, was the one that you were working with, which is not surprising.


解决方案是:



The solution was to do:

private void SaveBMP(ref Bitmap bmp) // now 'ref' parameter
{
    try    
    {       
        bmp.Save(_project.MapFilePath);    
    }    
    catch    
    {       
        Bitmap bitmap = new Bitmap(bmp.Width, bmp.Height, bmp.PixelFormat);
        Graphics g = Graphics.FromImage(bitmap);
        g.DrawImage(bmp, new Point(0,0));
        g.Dispose();        
        bmp.Dispose();       
        bitmap.Save(_project.MapFilePath);
        bmp = bitmap; // preserve clone        
    }
}





感谢您的帮助!



Thank you for the help!


这篇关于C#位图保存通用GDI +错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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