具體格式介紹,請參照以下文章:
Bmp文件格式
BMP文件格式分析
以下是代碼:
public class BmpParser { public static BmpInfo Parse(string path) { BmpInfo bmpInfo = new BmpInfo(); byte[] bmpBytes = File.ReadAllBytes(path); // 獲取bmp標識 bmpInfo.BmpMark = bmpBytes.ToStringEx(0, 2); // 獲取整個文件的大小 bmpInfo.FileSize = bmpBytes.ToInt32(2, 4); // 獲取保留 bmpInfo.Reserved = bmpBytes.ToInt32(6, 4); // 獲取從文件開始到位圖數據開始之間的數據之間的偏移量 bmpInfo.BitmapDataOffset = bmpBytes.ToInt32(10, 4); // 位圖信息頭(Bitmap Info Header)的長度 bmpInfo.BitmapHeaderSize = bmpBytes.ToInt32(14, 4); // 位圖的寬度,以象素爲單位 bmpInfo.Width = bmpBytes.ToInt32(18, 4); // 位圖的高度,以象素爲單位 bmpInfo.Height = bmpBytes.ToInt32(22, 4); // 位圖的位面數(注:該值將總是1) bmpInfo.Planes = bmpBytes.ToInt32(26, 2); // 每個象素的位數 bmpInfo.BitsPerPixel = bmpBytes.ToInt32(28, 2); // 壓縮 bmpInfo.Compression = bmpBytes.ToInt32(30, 4); // 位圖數據的大小 bmpInfo.BitmapDataSize = bmpBytes.ToInt32(34, 4); // 象素/米表示的水平分辨率 bmpInfo.HResolution = bmpBytes.ToInt32(38, 4); // 用象素/米表示的垂直分辨率 bmpInfo.VResolution = bmpBytes.ToInt32(42, 4); // 位圖使用的顏色數 bmpInfo.Colors = bmpBytes.ToInt32(46, 4); // 指定重要的顏色數 bmpInfo.ImportantColors = bmpBytes.ToInt32(50, 4); // 圖象數據 bmpInfo.BitmapData = bmpBytes.ToBytesList(bmpInfo.BitmapDataOffset, bmpInfo.FileSize - bmpInfo.BitmapDataOffset); // 獲取調色板 bmpInfo.Palette = bmpBytes.ToBytesList(54, bmpInfo.BitmapDataOffset - 54); return bmpInfo; } } public static class BytesEntend { public static int ToInt32(this byte[] bytes, int index, int count) { int result = 0; for (int i = count - 1; i >= 0; i--) { if (bytes[index + i] != 0) { result += (int)Math.Pow(256, i) * bytes[index + i]; } } return result; } public static string ToStringEx(this byte[] bytes, int index, int count) { return Encoding.Default.GetString(bytes, index, count); } public static List<byte> ToBytesList(this byte[] bytes, int index, int count) { return bytes.Skip(index).Take(count).ToList(); } } public class BmpInfo { /// <summary> /// bmp標識 /// </summary> public string BmpMark { get; set; } /// <summary> /// 用字節表示的整個文件的大小 /// </summary> public int FileSize { get; set; } /// <summary> /// 保留,必須設置爲0 /// </summary> public int Reserved { get; set; } /// <summary> /// 從文件開始到位圖數據開始之間的數據(bitmap data)之間的偏移量 /// </summary> public int BitmapDataOffset { get; set; } /// <summary> /// 位圖信息頭(Bitmap Info Header)的長度,用來描述位圖的顏色、壓縮方法等。下面的長度表示: /// 28h - Windows 3.1x, 95, NT, … 0Ch - OS/2 1.x F0h - OS/2 2.x /// 注:在Windows95、98、2000等操作系統中,位圖信息頭的長度並不一定是28h, /// 因爲微軟已經制定出了新的BMP文件格式,其中的信息頭結構變化比較大,長度加長。 /// 所以最好不要直接使用常數28h,而是應該從具體的文件中讀取這個值。這樣才能確保程序的兼容性. /// </summary> public int BitmapHeaderSize { get; set; } /// <summary> /// 位圖的寬度,以象素爲單位 /// </summary> public int Width { get; set; } /// <summary> /// 位圖的高度,以象素爲單位 /// </summary> public int Height { get; set; } /// <summary> /// 位圖的位面數(注:該值將總是1) /// </summary> public int Planes { get; set; } /// <summary> /// 每個象素的位數 /// 1 - 單色位圖(實際上可有兩種顏色,缺省情況下是黑色和白色。你可以自己定義這兩種顏色) /// 4 - 16 色位圖 /// 8 - 256 色位圖 /// 16 - 16bit 高彩色位圖 /// 24 - 24bit 真彩色位圖 /// 32 - 32bit 增強型真彩色位圖 /// </summary> public int BitsPerPixel { get; set; } /// <summary> /// 壓縮說明: /// 0 - 不壓縮 (使用BI_RGB表示) /// 1 - RLE 8-使用8位RLE壓縮方式(用BI_RLE8表示) /// 2 - RLE 4-使用4位RLE壓縮方式(用BI_RLE4表示) /// 3 - Bitfields-位域存放方式(用BI_BITFIELDS表示) /// </summary> public int Compression { get; set; } /// <summary> /// 用字節數表示的位圖數據的大小。該數必須是4的倍數 /// </summary> public int BitmapDataSize { get; set; } /// <summary> /// 用象素/米表示的水平分辨率 /// </summary> public int HResolution { get; set; } /// <summary> /// 用象素/米表示的垂直分辨率 /// </summary> public int VResolution { get; set; } /// <summary> /// 位圖使用的顏色數。如8-比特/象素表示爲100h或者 256. /// </summary> public int Colors { get; set; } /// <summary> /// 指定重要的顏色數。當該域的值等於顏色數時(或者等於0時),表示所有顏色都一樣重要 /// </summary> public int ImportantColors { get; set; } /// <summary> /// 調色板規範。對於調色板中的每個表項,這4個字節用下述方法來描述RGB的值: /// 1字節用於藍色分量 /// 1字節用於綠色分量 /// 1字節用於紅色分量 ///1字節用於填充符(設置爲0) /// </summary> public List<byte> Palette { get; set; } /// <summary> /// 該域的大小取決於壓縮方法及圖像的尺寸和圖像的位深度, /// 它包含所有的位圖數據字節,這些數據可能是彩色調色板的索引號, /// 也可能是實際的RGB值,這將根據圖像信息頭中的位深度值來決定。 /// </summary> public List<byte> BitmapData { get; set; } }
轉自大豆男生 using System;using System.Collections.Generic;using System.ServiceProcess;using System.Configuration.Insta