BMP图形文件分析类(c#)

1usingSystem;
2usingJJBase.FILE;
3namespaceJJBase.Image
4{
5/**////<summary>
6///BMP的摘要说明。
7///</summary>

8publicclassBMP
9{
10
11publicBMP()
12{
13//
14//TODO:在此处添加构造函数逻辑
15//作者:梁俊杰
16//时间:2005-9-29
17//功能:分析bmp文件格式
18//本文参考了林福宗老师的有关BMP文件格式的文章
19//参考链接:http://www.chinahacker.net/article/showarticle.asp?articleid=20809
20//参考链接:http://www.moon-soft.com/program/FORMAT/graphics/Bmp.html
21//
22}

23/**//*BMP(BitMap-File)图形文件是Windows采用的图形文件格式,在Windows环境
24*下运行的所有图象处理软件都支持BMP图象文件格式。Windows系统内部各
25*图像绘制操作都是以BMP为基础的。Windows3.0以前的BMP图文件格式与
26*显示设备有关,因此把这种BMP图象文件格式称为设备相关位图DDB
27*(device-dependentBitMap)文件格式。Windows3.0以后的BMP图象文件与
28*显示设备无关,因此把这种BMP图象文件格式称为设备无关位图DIB
29*(device-independentBitMap)格式(注:Windows3.0以后,在系统中仍
30*然存在DDB位图,象BitBlt()这种函数就是基于DDB位图的,只不过如果你想将
31*图像以BMP格式保存到磁盘文件中时,微软极力推荐你以DIB格式保存),目的
32*是为了让Windows能够在任何类型的显示设备上显示所存储的图象。BMP位图文件
33*默认的文件扩展名是BMP或者bmp(有时它也会以.DIB或.RLE作扩展名)。
34**/

35publicstructStructBMP
36{
37publicBMPHeaderHeader;
38publicBMPPalettePalette;
39publicBMPDataData;
40}

41publicstructBMPHeader
42{
43/**//*位图文件可看成由4个部分组成:位图文件头(BitMap-fileheader)、
44*位图信息头(BitMap-informationheader)、彩色表(colortable)和
45*定义位图的字节阵列,
46**/

47publicstringIdentifier;/**//*2bytes,识别位图的类型:
48‘BM’:Windows3.1x,95,NT,…
49‘BA’:OS/2BitMapArray
50‘CI’:OS/2ColorIcon
51‘CP’:OS/2ColorPointer
52‘IC’:OS/2Icon
53‘PT’:OS/2Pointer
54注:因为OS/2系统并没有被普及开,所以在编程时,你只需判断第一个标识“BM”就行。
55*/

56publicSystem.Int32FileSize;//1dword,用字节表示的整个文件的大小
57publicbyte[]Reserved;//1dword,保留,必须设置为0
58publicSystem.Int32BitMapDataOffset;//1dword,从文件开始到位图数据开始之间的数据(BitMapdata)之间的偏移量
59publicSystem.Int32BitMapHeaderSize;/**//*1dword
60位图信息头(BitMapInfoHeader)的长度,用来描述位图的颜色、压缩方法等。下面的长度表示:
6128h-windows3.1x,95,nt,…
620ch-os/21.x
63f0h-os/22.x
64注:在Windows95、98、2000等操作系统中,位图信息头的长度并不一定是28h,因为微软已经制定出了新的BMP文件格式,其中的信息头结构变化比较大,长度加长。所以最好不要直接使用常数28h,而是应该从具体的文件中读取这个值。这样才能确保程序的兼容性。
65*/

66publicSystem.Int32Width;//1dword,位图的宽度,以象素为单位
67publicSystem.Int32Height;//1dword,位图的高度,以象素为单位
68publicSystem.Int16Planes;//1word,位图的位面数(注:该值将总是1)
69publicSystem.Int16BitsPerPixel;
70/**//*1word
71每个象素的位数
721-单色位图(实际上可有两种颜色,缺省情况下是黑色和白色。你可以自己定义这两种颜色)
734-16色位图
748-256色位图
7516-16bit高彩色位图
7624-24bit真彩色位图
7732-32bit增强型真彩色位图
78*/

79publicSystem.Int32Compression;
80/**//*1dword
81压缩说明:
820-不压缩(使用BI_RGB表示)
831-RLE8-使用8位RLE压缩方式(用BI_RLE8表示)
842-RLE4-使用4位RLE压缩方式(用BI_RLE4表示)
853-Bitfields-位域存放方式(用BI_BITFIELDS表示)
86*/

87publicSystem.Int32BitMapDataSize;//1dword,用字节数表示的位图数据的大小。该数必须是4的倍数
88publicSystem.Int32HResolution;//1dword,用象素/米表示的水平分辨率
89publicSystem.Int32VResolution;//1dword,用象素/米表示的垂直分辨率
90publicSystem.Int32Colors;//1dword,位图使用的颜色数。如8-比特/象素表示为100h或者256.
91publicSystem.Int32ImportantColors;
92/**//*1dword,指定重要的颜色数。当该域的值等于颜色数时(或者等于0时),表示所有颜色都一样重要
93*/

94}

95publicstructBMPPalette
96{
97publicbyte[]Palette;//newbyte[8192];//bmp规范没有规定调色板最大81926字节,此处可以根据程序需要调节
98/**//*调色板数据根据BMP版本的不同而不同PaletteN*4byte调色板规范。
99对于调色板中的每个表项,这4个字节用下述方法来描述RGB的值:1字节用于蓝色分量
1001字节用于绿色分量
1011字节用于红色分量
1021字节用于填充符(设置为0)
103*/

104}

105publicstructBMPData
106{
107publicbyte[]BitMapData;//=newbyte[1024000];//bmp规范没有规定bmp数据最多为1024000,此处可以根据需要调整
108/**//*
109图象数据根据BMP版本及调色板尺寸的不同而不同BitMapDataxxxbytes该域的大小取决
110于压缩方法及图像的尺寸和图像的位深度,它包含所有的位图数据字节,这些数据可能是
111彩色调色板的索引号,也可能是实际的RGB值,这将根据图像信息头中的位深度值来决定。
112*/

113}

114publicvoidProcessBMP(refStructBMPsbmp,byte[]bytesFile)
115{
116byte[]word1=newbyte[2];
117byte[]word2=newbyte[4];
118System.Int32result;
119stringstr="";
120word1[0]=bytesFile[0];
121word1[1]=bytesFile[1];
122str=FromBytesToString(word1);
123sbmp.Header.Identifier=str;
124word2[0]=bytesFile[2];
125word2[1]=bytesFile[3];
126word2[2]=bytesFile[4];
127word2[3]=bytesFile[5];
128result=this.FromBytesToInt32(word2);
129sbmp.Header.FileSize=result;
130word2[0]=bytesFile[10];
131word2[1]=bytesFile[11];
132word2[2]=bytesFile[12];
133word2[3]=bytesFile[13];
134result=this.FromBytesToInt32(word2);
135sbmp.Header.BitMapDataOffset=result;
136word2[0]=bytesFile[14];
137word2[1]=bytesFile[15];
138word2[2]=bytesFile[16];
139word2[3]=bytesFile[17];
140result=this.FromBytesToInt32(word2);
141sbmp.Header.BitMapHeaderSize=result;
142word2[0]=bytesFile[18];
143word2[1]=bytesFile[19];
144word2[2]=bytesFile[20];
145word2[3]=bytesFile[21];
146sbmp.Header.Width=result;
147word2[0]=bytesFile[22];
148word2[1]=bytesFile[23];
149word2[2]=bytesFile[24];
150word2[3]=bytesFile[25];
151result=this.FromBytesToInt32(word2);
152sbmp.Header.Height=result;
153word1[0]=bytesFile[26];
154word1[1]=bytesFile[27];
155sbmp.Header.Planes=(System.Int16)FromBytesToInt32(word1);
156word1[0]=bytesFile[28];
157word1[1]=bytesFile[29];
158sbmp.Header.BitsPerPixel=(System.Int16)FromBytesToInt32(word1);
159word2[0]=bytesFile[30];
160word2[1]=bytesFile[31];
161word2[2]=bytesFile[32];
162word2[3]=bytesFile[33];
163result=this.FromBytesToInt32(word2);
164sbmp.Header.Compression=result;
165word2[0]=bytesFile[34];
166word2[1]=bytesFile[35];
167word2[2]=bytesFile[36];
168word2[3]=bytesFile[37];
169result=this.FromBytesToInt32(word2);
170sbmp.Header.BitMapDataSize=result;
171word2[0]=bytesFile[38];
172word2[1]=bytesFile[39];
173word2[2]=bytesFile[40

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值