C# C:“获取错误:”;GDI+;中发生一般性错误&引用;尝试将imagebox.Image复制到内存流时。此流未保存到文件。

C# C:“获取错误:”;GDI+;中发生一般性错误&引用;尝试将imagebox.Image复制到内存流时。此流未保存到文件。,c#,image,runtime-error,memorystream,C#,Image,Runtime Error,Memorystream,我在Image.save(流、格式)中遇到一个奇怪的错误 我试图在这里寻找解决方案,但每个人似乎都认为错误来自文件权限。在我的情况下,这不可能,因为流没有进入文件。我的代码如下: MemoryStream Stream = new MemoryStream(); this.Image_Box_1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg); TI.AlbumCover = Stream.ToArray(); Stream

我在
Image.save(流、格式)
中遇到一个奇怪的错误

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

MemoryStream Stream = new MemoryStream();
this.Image_Box_1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg);
TI.AlbumCover = Stream.ToArray();
Stream.Close();
TI.AlbumCover
是一个
字节[]

有人知道问题出在哪里吗

编辑:

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

因为我后来还需要覆盖该jpg文件,所以我不能简单地让它的filestream保持打开状态,所以我的其余代码保持不变,并将代码改为从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;
}