For details about the format, refer to the following article:
BMP file format
BMP file format Analysis
The following code is used:
Public class BMP parser <br/>{< br/> Public static BMP info parse (string path) <br/>{< br/> BMP info = new BMP Info (); <br/> byte [] BMP bytes = file. readallbytes (PATH); </P> <p> // obtain the BMP id <br/> BMP info. BMP mark = BMP bytes. tostringex (0, 2); <br/> // obtain the size of the entire file <br/> BMP info. filesize = BMP bytes. toint32 (2, 4); <br/> // get retained <br/> BMP info. reserved = BMP bytes. toint32 (6, 4); <br/> // gets the number of graph data starting from the file. Data offset <br/> BMP info. bitmapdataoffset = BMP bytes. toint32 (10, 4); <br/> // length of the bitmap info header <br/> BMP info. bitmapheadersize = BMP bytes. toint32 (14, 4); <br/> // The width of the bitmap, in pixels <br/> BMP info. width = BMP bytes. toint32 (18, 4); <br/> // height of the bitmap, in pixels <br/> BMP info. height = BMP bytes. toint32 (22, 4); <br/> // bitmap bit number (Note: This value will always be 1) <br/> BMP info. planes = BMP bytes. toint32 (26, 2); <br/> // The number of digits in each pixel <br/> BM Pinfo. bitsperpixel = BMP bytes. toint32 (28, 2); <br/> // compression <br/> BMP info. compression = BMP bytes. toint32 (30, 4); <br/> // the size of the bitmap data <br/> BMP info. bitmapdatasize = BMP bytes. toint32 (34, 4); <br/> // horizontal resolution in pixels/meters <br/> BMP info. hresolution = BMP bytes. toint32 (38, 4); <br/> // vertical resolution in pixels/meters <br/> BMP info. vresolution = BMP bytes. toint32 (42, 4); <br/> // number of colors used by the bitmap <br/> BMP info. colors = BMP bytes. toint32 (46, 4 ); <Br/> // specify the number of important colors <br/> BMP info. importantcolors = BMP bytes. toint32 (50, 4); <br/> // image data <br/> BMP info. bitmapdata = BMP bytes. tobyteslist (BMP info. bitmapdataoffset, BMP info. filesize-BMP info. bitmapdataoffset); <br/> // obtain the color palette <br/> BMP info. palette = BMP bytes. tobyteslist (54, BMP info. bitmapdataoffset-54); </P> <p> return BMP Info; <br/>}</P> <p> Public static class bytesentend <br/>{< br/> Pu BLIC static int toint32 (this byte [] bytes, int index, int count) <br/>{< br/> int result = 0; <br/> for (INT I = count-1; I >=0; I --) <br/>{< br/> If (Bytes [index + I]! = 0) <br/>{< br/> result + = (INT) math. pow (256, I) * bytes [index + I]; <br/>}</P> <p> return result; <br/>}</P> <p> Public static string tostringex (this byte [] bytes, int index, int count) <br/>{< br/> return encoding. default. getstring (bytes, index, count); <br/>}</P> <p> Public static list <byte> tobyteslist (this byte [] bytes, int index, int count) <br/>{< br/> return bytes. skip (index ). ta Ke (count ). tolist (); <br/>}</P> <p> public class BMP info <br/>{< br/> // <summary> <br/>/ // BMP id <br/> /// </Summary> <br/> Public String BMP mark <br/> {<br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // size of the entire file in bytes <br/> /// </Summary> <br/> Public int filesize <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // keep, must be set to 0 <br/> /// </Summary> <br/> Public Int reserved <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // bitmap data) offset between <br/> /// </Summary> <br/> Public int bitmapdataoffset <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // The length of the bitmap info header, used to describe the color and compression method of Bitmap. <Br/> // 28 h-Windows 3.1x, 95, NT ,... 0ch-OS/2 1.x f0h-OS/2 2.x< br/> // note: in Windows 95, 98, 2000, and other operating systems, the length of the bitmap information header is not necessarily 28 h. <br/> // because Microsoft has developed a new BMP file format, the structure of the Information header changes greatly, length. <Br/> // It is recommended that you do not directly use the constant for 28 h, but read the value from a specific file. This ensures program compatibility. <br/> // </Summary> <br/> Public int bitmapheadersize <br/> {<br/> get; <br/> set; <br/>}</P> <p> /// <summary> <br/> // width of the bitmap, in pixels <br/> /// </Summary> <br/> Public int width <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // height of the bitmap, in pixels <br/> /// </Summary> <br/> Public int height <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // bitmap number (Note: This The value will always be 1) <br/> /// </Summary> <br/> Public int planes <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // the number of bits in each pixel <br/> // 1-monochrome bitmap (There are actually two colors, black and white by default. You can define these two colors by yourself) <br/> // 4-16 color bitmap <br/> // 8-256 color bitmap <br/> // 16-16 Bit High color bitmap <br/> // 24-24bit true color bitmap <br/> // 32-32bit enhanced true color bitmap <br/> /// </Summary> <br/> Public int bitsperpixel <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // compression description: <br/> // 0-do not compress (expressed by bi_rgb) <br/> // 1-RLE 8-use the 8-bit RLE compression mode (represented by bi_rle8) <br/> // 2-RLE 4-use the 4-bit RLE compression method (expressed in bi_rle4) <br/> // 3-B Itfields-bit domain storage method (expressed in bi_bitfields) <br/> // </Summary> <br/> Public int compression <br/> {<br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // the size of the bitmap data expressed by the number of cells. This number must be a multiple of 4 <br/> /// </Summary> <br/> Public int bitmapdatasize <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // horizontal resolution in pixels/meters <br/> /// </Summary> <br/> Public int hresolution <br/> {<br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // The vertical resolution in pixels/meters <br/> /// </Summary> <br/> Public int vresolution <br/> {<br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // bitmap The number of colors used. For example, 8-bit/pixel represents 256 h or. <br/> // </Summary> <br/> Public int colors <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // specify the number of important colors. When the value of this field is equal to the number of colors (or equal to 0 ), indicates that all colors are equally important <br/> /// </Summary> <br/> Public int importantcolors <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // color palette specification. For each table item in the color palette, use the following methods to describe the values of RGB: <br/> // 1 byte is used for the blue component <br/> // 1 byte is used for the green component <br/> // 1 byte is used for the red component <br/> /// 1 byte for padding (set to 0) <br/> /// </Summary> <br/> public list <byte> palette <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // the size of the domain depends on the compression method, image size, and image position. depth, <br/> /// it contains all the bitmap data bytes, which may be the index number of the color palette, <br/> /// it may also be the actual RGB value, this is determined based on the bit depth value in the Image Information header. <Br/> // </Summary> <br/> public list <byte> bitmapdata <br/>{< br/> get; <br/> set; <br/>}< br/>}