简体   繁体   中英

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 我的形象

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM