BitmapFormat.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. namespace Sample
  5. {
  6. public class BitmapFormat
  7. {
  8. public struct BITMAPFILEHEADER
  9. {
  10. public ushort bfType;
  11. public int bfSize;
  12. public ushort bfReserved1;
  13. public ushort bfReserved2;
  14. public int bfOffBits;
  15. }
  16. public struct MASK
  17. {
  18. public byte redmask;
  19. public byte greenmask;
  20. public byte bluemask;
  21. public byte rgbReserved;
  22. }
  23. public struct BITMAPINFOHEADER
  24. {
  25. public int biSize;
  26. public int biWidth;
  27. public int biHeight;
  28. public ushort biPlanes;
  29. public ushort biBitCount;
  30. public int biCompression;
  31. public int biSizeImage;
  32. public int biXPelsPerMeter;
  33. public int biYPelsPerMeter;
  34. public int biClrUsed;
  35. public int biClrImportant;
  36. }
  37. /*******************************************
  38. * �������ƣ�RotatePic
  39. * �������ܣ���תͼƬ��Ŀ���DZ������ʾ��ͼƬ�밴��ָ�Ʒ���ͬ
  40. * ������Σ�BmpBuf---��תǰ��ָ���ַ���
  41. * �������Σ�ResBuf---��ת���ָ���ַ���
  42. * �������أ���
  43. *********************************************/
  44. public static void RotatePic(byte[] BmpBuf, int width, int height, ref byte[] ResBuf)
  45. {
  46. int RowLoop = 0;
  47. int ColLoop = 0;
  48. int BmpBuflen = width * height;
  49. try
  50. {
  51. for (RowLoop = 0; RowLoop < BmpBuflen;)
  52. {
  53. for (ColLoop = 0; ColLoop < width; ColLoop++)
  54. {
  55. ResBuf[RowLoop + ColLoop] = BmpBuf[BmpBuflen - RowLoop - width + ColLoop];
  56. }
  57. RowLoop = RowLoop + width;
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. //ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  63. //logger.Append();
  64. }
  65. }
  66. /*******************************************
  67. * �������ƣ�StructToBytes
  68. * �������ܣ����ṹ��ת�����޷����ַ�������
  69. * ������Σ�StructObj---��ת���Ľṹ��
  70. * Size---��ת���Ľṹ��Ĵ�С
  71. * ����������
  72. * �������أ��ṹ��ת���������
  73. *********************************************/
  74. public static byte[] StructToBytes(object StructObj, int Size)
  75. {
  76. int StructSize = Marshal.SizeOf(StructObj);
  77. byte[] GetBytes = new byte[StructSize];
  78. try
  79. {
  80. IntPtr StructPtr = Marshal.AllocHGlobal(StructSize);
  81. Marshal.StructureToPtr(StructObj, StructPtr, false);
  82. Marshal.Copy(StructPtr, GetBytes, 0, StructSize);
  83. Marshal.FreeHGlobal(StructPtr);
  84. if (Size == 14)
  85. {
  86. byte[] NewBytes = new byte[Size];
  87. int Count = 0;
  88. int Loop = 0;
  89. for (Loop = 0; Loop < StructSize; Loop++)
  90. {
  91. if (Loop != 2 && Loop != 3)
  92. {
  93. NewBytes[Count] = GetBytes[Loop];
  94. Count++;
  95. }
  96. }
  97. return NewBytes;
  98. }
  99. else
  100. {
  101. return GetBytes;
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. //ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  107. //logger.Append();
  108. return GetBytes;
  109. }
  110. }
  111. /*******************************************
  112. * �������ƣ�GetBitmap
  113. * �������ܣ��������������ݱ���ΪͼƬ
  114. * ������Σ�buffer---ͼƬ����
  115. * nWidth---ͼƬ�Ŀ���
  116. * nHeight---ͼƬ�ĸ߶�
  117. * ����������
  118. * �������أ���
  119. *********************************************/
  120. public static void GetBitmap(byte[] buffer, int nWidth, int nHeight, MemoryStream ms)
  121. {
  122. int ColorIndex = 0;
  123. ushort m_nBitCount = 8;
  124. int m_nColorTableEntries = 256;
  125. byte[] ResBuf = new byte[nWidth * nHeight * 2];
  126. try
  127. {
  128. BITMAPFILEHEADER BmpHeader = new BITMAPFILEHEADER();
  129. BITMAPINFOHEADER BmpInfoHeader = new BITMAPINFOHEADER();
  130. MASK[] ColorMask = new MASK[m_nColorTableEntries];
  131. int w = (((nWidth + 3) / 4) * 4);
  132. //ͼƬͷ��Ϣ
  133. BmpInfoHeader.biSize = Marshal.SizeOf(BmpInfoHeader);
  134. BmpInfoHeader.biWidth = nWidth;
  135. BmpInfoHeader.biHeight = nHeight;
  136. BmpInfoHeader.biPlanes = 1;
  137. BmpInfoHeader.biBitCount = m_nBitCount;
  138. BmpInfoHeader.biCompression = 0;
  139. BmpInfoHeader.biSizeImage = 0;
  140. BmpInfoHeader.biXPelsPerMeter = 0;
  141. BmpInfoHeader.biYPelsPerMeter = 0;
  142. BmpInfoHeader.biClrUsed = m_nColorTableEntries;
  143. BmpInfoHeader.biClrImportant = m_nColorTableEntries;
  144. //�ļ�ͷ��Ϣ
  145. BmpHeader.bfType = 0x4D42;
  146. BmpHeader.bfOffBits = 14 + Marshal.SizeOf(BmpInfoHeader) + BmpInfoHeader.biClrUsed * 4;
  147. BmpHeader.bfSize = BmpHeader.bfOffBits + ((((w * BmpInfoHeader.biBitCount + 31) / 32) * 4) * BmpInfoHeader.biHeight);
  148. BmpHeader.bfReserved1 = 0;
  149. BmpHeader.bfReserved2 = 0;
  150. ms.Write(StructToBytes(BmpHeader, 14), 0, 14);
  151. ms.Write(StructToBytes(BmpInfoHeader, Marshal.SizeOf(BmpInfoHeader)), 0, Marshal.SizeOf(BmpInfoHeader));
  152. //���԰���Ϣ
  153. for (ColorIndex = 0; ColorIndex < m_nColorTableEntries; ColorIndex++)
  154. {
  155. ColorMask[ColorIndex].redmask = (byte)ColorIndex;
  156. ColorMask[ColorIndex].greenmask = (byte)ColorIndex;
  157. ColorMask[ColorIndex].bluemask = (byte)ColorIndex;
  158. ColorMask[ColorIndex].rgbReserved = 0;
  159. ms.Write(StructToBytes(ColorMask[ColorIndex], Marshal.SizeOf(ColorMask[ColorIndex])), 0, Marshal.SizeOf(ColorMask[ColorIndex]));
  160. }
  161. //ͼƬ��ת�����ָ��ͼƬ����������
  162. RotatePic(buffer, nWidth, nHeight, ref ResBuf);
  163. byte[] filter = null;
  164. if (w - nWidth > 0)
  165. {
  166. filter = new byte[w - nWidth];
  167. }
  168. for (int i = 0; i < nHeight; i++)
  169. {
  170. ms.Write(ResBuf, i * nWidth, nWidth);
  171. if (w - nWidth > 0)
  172. {
  173. ms.Write(ResBuf, 0, w - nWidth);
  174. }
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. // ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  180. // logger.Append();
  181. }
  182. }
  183. /*******************************************
  184. * �������ƣ�WriteBitmap
  185. * �������ܣ��������������ݱ���ΪͼƬ
  186. * ������Σ�buffer---ͼƬ����
  187. * nWidth---ͼƬ�Ŀ���
  188. * nHeight---ͼƬ�ĸ߶�
  189. * ����������
  190. * �������أ���
  191. *********************************************/
  192. public static void WriteBitmap(byte[] buffer, int nWidth, int nHeight)
  193. {
  194. int ColorIndex = 0;
  195. ushort m_nBitCount = 8;
  196. int m_nColorTableEntries = 256;
  197. byte[] ResBuf = new byte[nWidth * nHeight];
  198. try
  199. {
  200. BITMAPFILEHEADER BmpHeader = new BITMAPFILEHEADER();
  201. BITMAPINFOHEADER BmpInfoHeader = new BITMAPINFOHEADER();
  202. MASK[] ColorMask = new MASK[m_nColorTableEntries];
  203. int w = (((nWidth + 3) / 4) * 4);
  204. //ͼƬͷ��Ϣ
  205. BmpInfoHeader.biSize = Marshal.SizeOf(BmpInfoHeader);
  206. BmpInfoHeader.biWidth = nWidth;
  207. BmpInfoHeader.biHeight = nHeight;
  208. BmpInfoHeader.biPlanes = 1;
  209. BmpInfoHeader.biBitCount = m_nBitCount;
  210. BmpInfoHeader.biCompression = 0;
  211. BmpInfoHeader.biSizeImage = 0;
  212. BmpInfoHeader.biXPelsPerMeter = 0;
  213. BmpInfoHeader.biYPelsPerMeter = 0;
  214. BmpInfoHeader.biClrUsed = m_nColorTableEntries;
  215. BmpInfoHeader.biClrImportant = m_nColorTableEntries;
  216. //�ļ�ͷ��Ϣ
  217. BmpHeader.bfType = 0x4D42;
  218. BmpHeader.bfOffBits = 14 + Marshal.SizeOf(BmpInfoHeader) + BmpInfoHeader.biClrUsed * 4;
  219. BmpHeader.bfSize = BmpHeader.bfOffBits + ((((w * BmpInfoHeader.biBitCount + 31) / 32) * 4) * BmpInfoHeader.biHeight);
  220. BmpHeader.bfReserved1 = 0;
  221. BmpHeader.bfReserved2 = 0;
  222. Stream FileStream = File.Open("finger.bmp", FileMode.Create, FileAccess.Write);
  223. BinaryWriter TmpBinaryWriter = new BinaryWriter(FileStream);
  224. TmpBinaryWriter.Write(StructToBytes(BmpHeader, 14));
  225. TmpBinaryWriter.Write(StructToBytes(BmpInfoHeader, Marshal.SizeOf(BmpInfoHeader)));
  226. //���԰���Ϣ
  227. for (ColorIndex = 0; ColorIndex < m_nColorTableEntries; ColorIndex++)
  228. {
  229. ColorMask[ColorIndex].redmask = (byte)ColorIndex;
  230. ColorMask[ColorIndex].greenmask = (byte)ColorIndex;
  231. ColorMask[ColorIndex].bluemask = (byte)ColorIndex;
  232. ColorMask[ColorIndex].rgbReserved = 0;
  233. TmpBinaryWriter.Write(StructToBytes(ColorMask[ColorIndex], Marshal.SizeOf(ColorMask[ColorIndex])));
  234. }
  235. //ͼƬ��ת�����ָ��ͼƬ����������
  236. RotatePic(buffer, nWidth, nHeight, ref ResBuf);
  237. //дͼƬ
  238. //TmpBinaryWriter.Write(ResBuf);
  239. byte[] filter = null;
  240. if (w - nWidth > 0)
  241. {
  242. filter = new byte[w - nWidth];
  243. }
  244. for (int i = 0; i < nHeight; i++)
  245. {
  246. TmpBinaryWriter.Write(ResBuf, i * nWidth, nWidth);
  247. if (w - nWidth > 0)
  248. {
  249. TmpBinaryWriter.Write(ResBuf, 0, w - nWidth);
  250. }
  251. }
  252. FileStream.Close();
  253. TmpBinaryWriter.Close();
  254. }
  255. catch (Exception ex)
  256. {
  257. //ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  258. //logger.Append();
  259. }
  260. }
  261. }
  262. }