覆盖图像文件(位图) [英] Overwriting an image file (bitmap)
问题描述
您好,我试图挽救一个位图图像在一个基本的图像编辑器程序。
Hello I am trying to save a bitmap image in a basic image editor program.
这里的code:
// then save it
ImageBoxInApp.Image.Save(filename);
并我打开这个图像
openFileDialog1.Title = "Select an Image";
openFileDialog1.Filter = "All Files|*.*|Windows Bitmaps|*.bmp|JPEG Files|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog1.FileName;
Image img = System.Drawing.Image.FromFile(filename);
所以,当我尝试这个我收到通用GDI +误差
。所以,我发现了一个解决来此,它看起来是这样的:
So when I try this I receive a Generic GDI+ error
. So I found a solution to this and it looks like this:
// Delete existing file first
System.IO.File.Delete(filename);
// then save it
ImageBoxInApp.Image.Save(filename);
但是,当我尝试这样做,我收到另一个错误说我删除该文件是当前打开的。这是因为这是我想编辑的文件。
But when I try to do this I receive another error saying that the file I am deleting is currently open. This is because that is the file that I am trying to edit.
我如何关闭实际上不关闭应用程序的文件?或者是有一个替代的解决方案?
How do I "close" the file without actually closing the application? Or is there an alternative solution?
谢谢!
推荐答案
它很难说没有看到周围的你是如何加载文件的一些方面,但这里有一些建议:
Its hard to say without seeing some context around how your loading the file, but here are a couple of suggestions:
- 保存之前,现有的图像复制到一个新的图像,关闭原来,执行删除,并保存副本。
- 将文件保存到一个随机的文件名,删除原来,随机命名的文件重命名为原来的名称。
- 将文件加载到内存流,并使用在内存中拷贝初始化图像。
我会亲自去与选项#3在大多数情况下。一定要处理的图像,当你使用它完成 - 这是最好的,如果你能在使用()块把它包
I'd personally go with option #3 for most cases. Be sure to dispose of the image when you've finished using it - it is best if you can wrap it in a using ( ) block.
这篇关于覆盖图像文件(位图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!