Reputation: 51
I know there is a lot of help already about this subject, but using this help I ran into a problem: I have an image list with icons that I display in a tree view on a windows form and that works.
Now I want to take these icons, put them in a table, clear the image list and then load the image list from the table. This is to test if I can use a table to maintain the icons.
The problem is that the icons display a black background using this method. I use BytesFromImage
to write to an Image
column in the table and ImageFromStream
and StreamFromBytes
to read the data back to an image.
What am I missing?
public static byte[] BytesFromImage(Image TheImage)
{
MemoryStream ms = new MemoryStream();
TheImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public static Stream StreamFromBytes(byte[] DataBytes)
{
MemoryStream stream = new MemoryStream();
stream.Write(DataBytes, 0, DataBytes.Length);
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
public static Image ImageFromStream(Stream DataStream)
{
return Bitmap.FromStream(DataStream);
}
Upvotes: 5
Views: 733
Reputation: 51
I played around and changing the format to png solved the problem. Sorry for the unnecessary post.
Upvotes: 0
Reputation: 514
The black background is caused by converting the icons into the format GIF. Better use PNG, this should respect transparent backgrounds:
public static byte[] BytesFromImage(Image TheImage)
{
MemoryStream ms = new MemoryStream();
TheImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
Upvotes: 1
Reputation: 397
Actually there's nothing wrong with your code. The problem is format specification. I have run into this problem when i was trying to convert an image which I created from Photoshop with a format of PNG to JPEG/JPG. According to popular documentation like Microsoft, PNG images retain high detail of information of an image, JPG doesn't. JPEG is not good as a compression option when you want to compress images that have high transitional borders. Especially when porting from PNG to JPEG. You'll always notice that. I think the problem is that you are using a raw bitmap. Try parsing it to Image
instead of Bitmap
and see if this works
Image image = System.Drawing.Image.FromStream(stream);
Upvotes: 2