我正在使用下面的代码编写关于图像转换为gif格式的代码。我收到错误消息“无法访问已关闭的流。”
protected void Button1_Click(object sender, EventArgs e)
{
//Variable declaration
MemoryStream memoryStream;
BinaryWriter binaryWriter;
System.Drawing.Image image;
Byte[] buf1;
//Variable declaration
MemoryStream stream = new MemoryStream();
memoryStream = new MemoryStream();
binaryWriter = new BinaryWriter(stream);
string[] fileEntries = Directory.GetFiles(@"C:\Users\OM\Documents\Visual Studio 2012\WebSites\WebSite6\img\");
int numFiles = fileEntries.Length;
for (int picCount = 0; picCount < numFiles; picCount++)
{
image = System.Drawing.Image.FromFile(fileEntries[picCount]);
image.Save(memoryStream, ImageFormat.Gif);
buf1 = memoryStream.ToArray();
memoryStream.SetLength(0);
}
binaryWriter.Close();
System.Drawing.Image streamGIF = System.Drawing.Image.FromStream(stream);
Label1.Text += "<img src=" + streamGIF + " /><br />";
}
答案 0 :(得分:0)
您发布的代码毫无意义。
MemoryStream
个对象(stream
和memoryStream
); BinaryWriter
; stream
Image
; Image
格式将memoryStream
保存到Gif
; memoryStream
的内容复制到字节数组; memoryStream
; binaryWriter
(也关闭stream
); stream
变量中加载图像; Label1
的文字设为<img src="System.Drawing.Image" /><br />
; 异常的直接问题可以通过在尝试从流中读取之前不关闭BinaryWriter
对象来解决。
但是,这不会改变您的代码不会向stream
或binaryWriter
写入任何内容的事实,因此Image.FromStream
方法将无效。
它也不会改变您无法将System.Drawing.Image
用作HTML src
代码的<img>
属性这一事实。