覆盖图像文件(位图)
您好,我正在尝试在基本图像编辑器程序中保存位图图像。
这是代码:
// 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+ 错误
。 所以我找到了一个解决方案,它看起来像这样:
// Delete existing file first
System.IO.File.Delete(filename);
// then save it
ImageBoxInApp.Image.Save(filename);
但是当我尝试这样做时我收到另一个错误,指出我要删除的文件当前已打开。 这是因为这是我要编辑的文件。
如何在不实际关闭应用程序的情况下“关闭”文件? 或者有其他解决方案吗?
谢谢!
Hello I am trying to save a bitmap image in a basic image editor program.
Here's the code:
// then save it
ImageBoxInApp.Image.Save(filename);
[EDIT] And I am opening the image with this
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);
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?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有看到有关如何加载文件的一些上下文,很难说,但这里有一些建议:
对于大多数情况,我个人会选择#3。 使用完图像后,请务必将其丢弃 - 最好将其包装在 using ( ) 块中。
Its hard to say without seeing some context around how your loading the file, but here are a couple of suggestions:
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.