覆盖图像文件(位图)

发布于 2024-07-18 08:47:27 字数 976 浏览 5 评论 0原文

您好,我正在尝试在基本图像编辑器程序中保存位图图像。

这是代码:

         // 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 技术交流群。

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

发布评论

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

评论(1

奢欲 2024-07-25 08:47:27

如果没有看到有关如何加载文件的一些上下文,很难说,但这里有一些建议:

  1. 在保存之前,将现有图像复制到新图像中,关闭原始图像,执行删除,然后保存副本。
  2. 将文件保存为随机文件名,删除原始文件,将随机命名的文件重命名为原始名称。
  3. 将文件加载到内存流中并使用内存中的副本来初始化图像。

对于大多数情况,我个人会选择#3。 使用完图像后,请务必将其丢弃 - 最好将其包装在 using ( ) 块中。

Its hard to say without seeing some context around how your loading the file, but here are a couple of suggestions:

  1. Before saving, copy the existing image into a new Image, close the original, perform the delete, and save the copy.
  2. Save the file to a random filename, delete the original, rename the randomly named file to the original name.
  3. Load the file into a memory stream and use the in memory copy to initialize the image.

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.

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