C# 从数组中保存图像

发布于 2024-10-31 12:44:15 字数 486 浏览 8 评论 0原文

我有一个已调整大小的图像数组,我想做的是直接从数组中保存它们...

foreach (Image I in Resizedimages)
            {
                string f = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString() + "\\NewImages\\" + names[n];

                I.Save(f, System.Drawing.Imaging.ImageFormat.Jpeg);
                n++;

            }

问题是每次运行程序时都会收到未处理的异常“GDI+ 中发生一般错误”并且我知道这与 save 方法有关。我假设这个问题以前已经被问过,如果是这样的话,我很抱歉。在网上搜索了几个小时后,我尝试了许多不同的修复方法,我想这可能是我的程序所特有的。有什么想法吗?

I have an array of images I have resized, what I'm trying to do is save them straight from the array...

foreach (Image I in Resizedimages)
            {
                string f = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString() + "\\NewImages\\" + names[n];

                I.Save(f, System.Drawing.Imaging.ImageFormat.Jpeg);
                n++;

            }

The problem is EVERY TIME I run the program I get an unhandled exception "A generic error occurred in GDI+" and I know for a fact that it is something to do with the save method. I'm assuming this question has been asked before, and if that is the case then I am sorry. I have tried many different fixes after searching for hours online I thought that maybe it's specific to my program. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

骑趴 2024-11-07 12:44:15

问题可能是位图对象或图形对象锁定了某些内容。对某些内容调用 Dispose() 可能会起作用,或者您可以复制到新的位图并保存它:

       Bitmap copy = new Bitmap(I);
       copy.Save(f, System.Drawing.Imaging.ImageFormat.Jpeg);

为了避免 GDI 锁定文件,您可以从 MemoryStream 创建图像:

       MemoryStream stream = new MemoryStream(File.ReadAllBytes(fileName));
       Image image = Image.FromStream(stream);

The problem may be with something being locked, either by the Bitmap object or the Graphics object. Calling Dispose() on something might work or you can just copy to a new bitmap and save that:

       Bitmap copy = new Bitmap(I);
       copy.Save(f, System.Drawing.Imaging.ImageFormat.Jpeg);

To avoid file-locking by GDI, you can create an image from a MemoryStream:

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