No transperancy in Bitmap loading from MemoryStream
- by Jogi Joseph George
Please see the C# code. When i am writing a Bitmap to a file and read from the file, i am getting the transperancy correctly.
using (Bitmap bmp = new Bitmap(2, 2))
{
Color col = Color.FromArgb(1, 2, 3, 4);
bmp.SetPixel(0, 0, col);
bmp.Save("J.bmp");
}
using (Bitmap bmp = new Bitmap("J.bmp"))
{
Color col = bmp.GetPixel(0, 0);
// ------------------------------
// Here col.A is 1. This is right.
// ------------------------------
}
But if I write the Bitmap to a MemoryStream and read from that MemoryStream, the transperancy has been removed. All Alpha values become 255.
MemoryStream ms = new MemoryStream();
using (Bitmap bmp = new Bitmap(2, 2))
{
Color col = Color.FromArgb(1, 2, 3, 4);
bmp.SetPixel(0, 0, col);
bmp.Save(ms, ImageFormat.Bmp);
}
using (Bitmap bmp = new Bitmap(ms))
{
Color col = bmp.GetPixel(0, 0);
// ------------------------------
// But here col.A is 255. Why? i am expecting 1 here.
// ------------------------------
}
I wish to save the Bitmap to a MemoryStream and read it back with transperancy.
Could you please help me?