保存一个修改的图像,以使用GDI原始文件+ [英] Saving a modified image to the original file using GDI+

查看:165
本文介绍了保存一个修改的图像,以使用GDI原始文件+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被加载从文件中位图图像。当我试图将图像保存到另一个文件中,我得到了下面的错误GDI +中发生一般性错误。我相信这是因为该文件是由图像对象锁定。

I was loading a Bitmap Image from a File. When I tried to save the Image to another file I got the following error "A generic error occurred in GDI+". I believe this is because the file is locked by the image object.

确定这样的尝试调用Image.Clone功能。这仍然锁定该文件。

Ok so tried calling the Image.Clone function. This still locks the file.

哼。接下来,我尝试从一个FileStream加载位图图像,图像加载到内存中,因此GDI +不锁定文件。但我需要生成使用它抛出一个内存溢出异常Image.GetThumbnailImage方法缩略图这个伟大的工程。显然,我需要保持流开停此异常,但如果我把流,然后打开该文件保持锁定状态。

hmm. Next I try loading a Bitmap Image from a FileStream and load the image into memory so GDI+ doesn't lock the file. This works great except I need to generate thumbnails using Image.GetThumbnailImage method it throws an out of memory exception. Apparently I need to keep the stream open to stop this exception but if I keep the stream open then the file remains locked.

因此,没有很好的与该方法。最后,我创建的文件的副本。所以现在我有2个版本的文件。 1我可以锁定在我的C#程序操作。这等原始文件保持未锁定状态,而我可以保存修改。这有让我甚为他们节省,因为我操纵的文件,它不能改变的副本后还原更改的奖金。

So no good with that method. In the end I created a copy of the file. So now I have 2 versions of the file. 1 I can lock and manipulate in my c# program. This other original file remains unlocked to which I can save modifications to. This has the bonus of allowing me to revert changes even after saving them because I'm manipulating the copy of the file which cant change.

当然,还有一个更好的方法无需实现这一有2个版本的图像文件。任何想法?

Surely there is a better way of achieving this without having to have 2 versions of the image file. Any ideas?

推荐答案

因为我已经找到了另一种方法来克隆图像不锁定文件。 鲍勃·鲍威尔拥有这一切以及更多GDI资源

I have since found an alternative method to clone the image without locking the file. Bob Powell has it all plus more GDI resources.

      //open the file
      Image i = Image.FromFile(path);

      //create temporary
      Image t=new Bitmap(i.Width,i.Height);

      //get graphics
      Graphics g=Graphics.FromImage(t);

      //copy original
      g.DrawImage(i,0,0);

      //close original
      i.Dispose();

      //Can now save
      t.Save(path)

这篇关于保存一个修改的图像,以使用GDI原始文件+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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