No transperancy in Bitmap loading from MemoryStream

Posted by Jogi Joseph George on Stack Overflow See other posts from Stack Overflow or by Jogi Joseph George
Published on 2012-09-18T15:08:48Z Indexed on 2012/09/18 15:37 UTC
Read the original article Hit count: 272

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about bitmap