Aht
Aht

Reputation: 593

Converting Png to Bmp and bitmap array

I search for my problem solution in SO and I don't find ( maybe it is not a problem and it works fine just I'm to dump to get it )

I have BMP file that i try to convert into bitmap array. Everything is fine, but i get an output file that looks weird. The file is 16x32, so i should get 512 bit. The final image is black and white, so i should have 512 x 3 ( 3 color bit ) pixels - 1536 pixels with value 0 or 255, but a get 1590 pixels. This 54 pixels have different value than 0 or 255 why ? What is that value and for what bmp file use it ? Code: `

        long time = 0;
        Stopwatch watch = new Stopwatch();
        watch.Start();
        Image img = Image.FromFile("test.png");
        byte[] data;
        MemoryStream ms = new MemoryStream();
        img.Save(ms, ImageFormat.Bmp);
        data = ms.ToArray();
        watch.Stop();
        time = watch.ElapsedTicks;
        Console.WriteLine(time);
        FileStream file = new FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite);
        StreamWriter writer = new StreamWriter(file);


        foreach (byte b in data)
        {
            writer.WriteLine(b); 
        }
        writer.Close();
        file.Close();
        Console.ReadKey();`

Code is not nice to read i know but is only for some test My image

Upvotes: 0

Views: 1174

Answers (1)

GinSonic
GinSonic

Reputation: 80

I would say its the fileheader, like TaW already pointed out. According to this website http://www.fastgraph.com/help/bmp_header_format.html the BMP header size is 54 bytes. If you look at offset 18 and 22, you should see the width and height (16, 32) of your picture.

Upvotes: 1

Related Questions