C#:出现错误:“GDI 中发生一般错误”当尝试将 imagebox.Image 复制到内存流时。该流未保存到文件中。

发布于 2024-11-04 02:03:45 字数 1629 浏览 3 评论 0原文

我在使用 Image.save(Stream, Format) 时遇到了奇怪的错误。

我尝试在这里寻找解决方案,但每个人似乎都认为错误来自文件权限。在我的情况下不可能是这样,流不会进入文件。我的代码如下:

MemoryStream Stream = new MemoryStream();
this.Image_Box_1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg);
TI.AlbumCover = Stream.ToArray();
Stream.Close();

TI.AlbumCover 是一个byte[]

有谁对可能出现的问题有任何想法吗?

编辑:

好的,所以我解决了。原始文件有时可能来自 jpg 文件,有时来自字节数组(id3 标记的一部分)。问题是,当图像来自文件时,我在创建图像框图像后关闭流。虽然图像仍然可见,但数据不再可用。

由于我后来还需要覆盖该 jpg 文件,因此我不能简单地使其文件流保持打开状态,因此我将其余代码保持不变,并将从 jpg 读取的代码更改为以下内容:

FileStream FS = new FileStream(File, FileMode.Open, FileAccess.Read);//Read in the jpg file
Image IMG = Image.FromStream(FS);//Create an image from the file data
MemoryStream MS = new MemoryStream();
IMG.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);//Save the image data to a memory stream
byte[] temp = MS.ToArray();//Copy the image data to a byte array
//close the streams
MS.Close(); 
FS.Close();
return temp; //was originally returning an image

然后在执行此代码后,我将将图像放入图像框中的代码更改为:

try
{
    if (this.m_V2Tag.AlbumCover != null)
        this.Image_Box_1.Image = Image.FromStream(new MemoryStream(this.m_V2Tag.AlbumCover));
    //changed code
    else
    {
        MemoryStream temp = new MemoryStream(this.getFolderJpg()); //create a memory stream from the byte[]. This stream can safely be left open.
        this.Image_Box_1.Image = Image.FromStream(temp); // create image and assign it to the image box
    }
}
catch
{
    this.Image_Box_1.Image = null;
}

I am getting an odd error with Image.save(Stream, Format).

I tried looking around on here for a solution but everyone seems to think the error is from file permissions. That can't be it in my case case the Stream isn't going into a file. My code is below:

MemoryStream Stream = new MemoryStream();
this.Image_Box_1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg);
TI.AlbumCover = Stream.ToArray();
Stream.Close();

TI.AlbumCover is a byte[].

Does anyone have any ideas on what the problem might be?

EDIT:

Ok, so I worked it out. The original file could sometimes come from a jpg file, sometimes from a byte array (part of an id3 tag). The problem was that when the image came from the file, I was closing the stream after creating the image box image. While the image remained visible, the data was no longer available.

Since I also later needed to overwrite that jpg file, I could not simply leave the filestream for it open so I left the rest of my code the same and changed the code to read from the jpg to the following:

FileStream FS = new FileStream(File, FileMode.Open, FileAccess.Read);//Read in the jpg file
Image IMG = Image.FromStream(FS);//Create an image from the file data
MemoryStream MS = new MemoryStream();
IMG.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);//Save the image data to a memory stream
byte[] temp = MS.ToArray();//Copy the image data to a byte array
//close the streams
MS.Close(); 
FS.Close();
return temp; //was originally returning an image

Then after executing this code I change the code that placed the image into the image box to:

try
{
    if (this.m_V2Tag.AlbumCover != null)
        this.Image_Box_1.Image = Image.FromStream(new MemoryStream(this.m_V2Tag.AlbumCover));
    //changed code
    else
    {
        MemoryStream temp = new MemoryStream(this.getFolderJpg()); //create a memory stream from the byte[]. This stream can safely be left open.
        this.Image_Box_1.Image = Image.FromStream(temp); // create image and assign it to the image box
    }
}
catch
{
    this.Image_Box_1.Image = null;
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文