Image.save(Stream, Format)
我收到了一个奇怪的错误。
我试着在这里寻找解决方案但是每个人似乎都认为错误来自文件权限。这不可能是在我的情况下,Stream不会进入文件。我的代码如下:
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;
}