Generic GDI+ error
-
I am using image streams to set the images of my toolbar. If I close the streams, I get generic GRI+ error. On the other hand if I dont close them, the error is resolved. Any ideas?
-
I am using image streams to set the images of my toolbar. If I close the streams, I get generic GRI+ error. On the other hand if I dont close them, the error is resolved. Any ideas?
Straight from MSDN
Image.FromStream
docs:Remarks You must keep the stream open for the lifetime of the Image object.
I created a Bitmap helper class that copied the bytes to MemoryStream and managed the Bitmap from there. Here are some of the methods of the class:public void Load(Stream sm) { Clear(); int len = (int) sm.Length; byte[] buf = new byte[len]; sm.Read(buf,0,len); _ms = new MemoryStream(buf); _bm = (Bitmap) Image.FromStream(_ms); } public void Load(string fileName) { FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read); Load(fs); fs.Close(); } public void Load(object dbField) { Clear(); if (dbField != DBNull.Value) { byte[] buf = (byte[])dbField; _ms = new MemoryStream(buf); _bm = (Bitmap) Image.FromStream(_ms); } } public void Clear() { if (_bm != null) { _bm.Dispose(); _bm = null; } if (_ms != null) { _ms.Close(); _ms = null; } } public Bitmap Bitmap { get {return _bm;} }
-
Straight from MSDN
Image.FromStream
docs:Remarks You must keep the stream open for the lifetime of the Image object.
I created a Bitmap helper class that copied the bytes to MemoryStream and managed the Bitmap from there. Here are some of the methods of the class:public void Load(Stream sm) { Clear(); int len = (int) sm.Length; byte[] buf = new byte[len]; sm.Read(buf,0,len); _ms = new MemoryStream(buf); _bm = (Bitmap) Image.FromStream(_ms); } public void Load(string fileName) { FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read); Load(fs); fs.Close(); } public void Load(object dbField) { Clear(); if (dbField != DBNull.Value) { byte[] buf = (byte[])dbField; _ms = new MemoryStream(buf); _bm = (Bitmap) Image.FromStream(_ms); } } public void Clear() { if (_bm != null) { _bm.Dispose(); _bm = null; } if (_ms != null) { _ms.Close(); _ms = null; } } public Bitmap Bitmap { get {return _bm;} }
Thanks a lot.