Image.Save(..)拋出GDI +異常,因?yàn)閮?nèi)存流已關(guān)閉我有一些二進(jìn)制數(shù)據(jù),我想保存為圖像。當(dāng)我嘗試保存圖像時(shí),如果用于創(chuàng)建圖像的內(nèi)存流在保存之前關(guān)閉,則會(huì)引發(fā)異常。我這樣做的原因是因?yàn)槲艺趧?dòng)態(tài)創(chuàng)建圖像,因此我需要使用內(nèi)存流。這是代碼:[TestMethod]public void TestMethod1(){
// Grab the binary data.
byte[] data = File.ReadAllBytes("Chick.jpg");
// Read in the data but do not close, before using the stream.
Stream originalBinaryDataStream = new MemoryStream(data);
Bitmap image = new Bitmap(originalBinaryDataStream);
image.Save(@"c:\test.jpg");
originalBinaryDataStream.Dispose();
// Now lets use a nice dispose, etc...
Bitmap2 image2;
using (Stream originalBinaryDataStream2 = new MemoryStream(data))
{
image2 = new Bitmap(originalBinaryDataStream2);
}
image2.Save(@"C:\temp\pewpew.jpg"); // This throws the GDI+ exception.}有沒(méi)有人對(duì)如何在關(guān)閉流的情況下保存圖像有任何建議?我不能依賴(lài)開(kāi)發(fā)人員記住在保存圖像后關(guān)閉流。實(shí)際上,開(kāi)發(fā)人員沒(méi)有使用內(nèi)存流生成映像的IDEA(因?yàn)樗l(fā)生在其他地方的其他代碼中)。我真的很困惑:(
Image.Save(..)拋出GDI +異常,因?yàn)閮?nèi)存流已關(guān)閉
胡說(shuō)叔叔
2019-07-26 15:14:47