Guest User

Untitled

a guest
May 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed)
  2. BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
  3. // Write my data into bmpData.Scan0
  4. bmp.UnlockBits(bmpData);
  5.  
  6. const int XPelsPerMeter = 0xb12; // 72 ppi, 96 would work well too
  7. const int YPelsPerMeter = 0xb12;
  8. const int Gptr = 0x40;
  9. const int Srccopy = 0x00CC0020;
  10.  
  11.  
  12. struct BITMAPFILEHEADER
  13. {
  14. public ushort bfType;
  15. public uint bfSize;
  16. public ushort bfReserved1;
  17. public ushort bfReserved2;
  18. public uint bfOffBits;
  19. }
  20.  
  21. struct BITMAPINFOHEADER
  22. {
  23. public uint biSize;
  24. public int biWidth;
  25. public int biHeight;
  26. public ushort biPlanes;
  27. public ushort biBitCount;
  28. public uint biCompression;
  29. public uint biSizeImage;
  30. public int biXPelsPerMeter;
  31. public int biYPelsPerMeter;
  32. public uint biClrUsed;
  33. public uint biClrImportant;
  34. }
  35.  
  36. public static byte[] GetByteArray(Bitmap bitmap)
  37. {
  38. IntPtr hbm = bitmap.GetHbitmap(); // this is step (1)
  39. IntPtr sdc = GetDC( IntPtr.Zero ); // First we obtain the DC for the screen
  40. // Next, create a DC for the original hbitmap
  41. IntPtr hdc = CreateCompatibleDC( sdc );
  42. SelectObject( hdc, hbm );
  43.  
  44. byte[] arrayBytes = CreateBinary(hdc, bitmap.Height, bitmap.Width);
  45.  
  46. // Finally some cleanup.
  47. DeleteDC( hdc );
  48. ReleaseDC( IntPtr.Zero, sdc );
  49. DeleteObject( hbm );
  50.  
  51. return arrayBytes;
  52. }
  53.  
  54. static int WIDTHBYTES( int bits )
  55. {
  56. return ( ( ( ( bits ) + 31 ) / 32 ) * 4 );
  57. }
  58.  
  59. private static byte[] CreateBinary( IntPtr hDc, int height, int width )
  60. {
  61. IntPtr hMemDc = CreateCompatibleDC( hDc );
  62.  
  63. int cb = 0;
  64.  
  65. BITMAPINFOHEADER bi = new BITMAPINFOHEADER();
  66. bi.biSize = ( uint )Marshal.SizeOf( bi );
  67. bi.biBitCount = 1; // Creating RGB bitmap. The following three members don't matter
  68. bi.biClrUsed = 2;
  69. bi.biClrImportant = 2;
  70. bi.biCompression = 0;
  71. bi.biHeight = height;
  72. bi.biWidth = width;
  73. bi.biPlanes = 1;
  74. cb = WIDTHBYTES( bi.biWidth * bi.biBitCount ) * bi.biHeight;
  75. bi.biSizeImage = ( uint )cb;
  76. bi.biXPelsPerMeter = XPelsPerMeter;
  77. bi.biYPelsPerMeter = YPelsPerMeter;
  78.  
  79. IntPtr pBits = IntPtr.Zero;
  80. //Allocate memory for bitmap bits
  81. IntPtr pBi = LocalAlloc( Gptr, bi.biSize );
  82. // Not sure if this needed - simply trying to keep marshaller happy
  83. Marshal.StructureToPtr( bi, pBi, false );
  84. //This will return IntPtr to actual DIB bits in pBits
  85. IntPtr hBmp = CreateDIBSection( hDc, pBi, 0, ref pBits, IntPtr.Zero, 0 );
  86. //Marshall back - now we have BITMAPINFOHEADER correctly filled in
  87. //Marshal.PtrToStructure(pBI, bi);
  88. BITMAPINFOHEADER biNew = ( BITMAPINFOHEADER )Marshal.PtrToStructure( pBi, typeof( BITMAPINFOHEADER ) );
  89.  
  90. //Usual stuff
  91. IntPtr hOldBitmap = SelectObject( hMemDc, hBmp );
  92. //Grab bitmap
  93. BitBlt( hMemDc, 0, 0, bi.biWidth, bi.biHeight, hDc, 0, 0, Srccopy );
  94. // Allocate memory for a copy of bitmap bits
  95. byte[] RealBits = new byte[cb];
  96. // And grab bits from DIBSestion data
  97. Marshal.Copy( pBits, RealBits, 0, cb );
  98.  
  99. // This simply creates valid bitmap file header, so it can be saved to disk
  100. BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
  101. uint colorSize = 2 * 4;//2 colors for B&W, 4 bytes (RGBQUAD)
  102. uint sizeofBinfo = 0x36 + colorSize;//original
  103. //sizeofBINFO = (uint)Marshal.SizeOf(bi);//sorin
  104. //bfh.bfSize = ( uint )cb + 0x36; // Size of header + size of BITMAPINFOHEADER size of bitmap bits
  105. bfh.bfSize = ( uint )( cb + sizeofBinfo );
  106. bfh.bfType = 0x4d42; //BM
  107. bfh.bfOffBits = sizeofBinfo; //
  108. int HdrSize = 14;
  109. byte[] header = new byte[HdrSize];
  110. BitConverter.GetBytes( bfh.bfType ).CopyTo( header, 0 );
  111. BitConverter.GetBytes( bfh.bfSize ).CopyTo( header, 2 );
  112. BitConverter.GetBytes( bfh.bfOffBits ).CopyTo( header, 10 );
  113.  
  114. //Allocate enough memory for complete bitmap file
  115. byte[] data = new byte[cb + bfh.bfOffBits];
  116. //BITMAPFILEHEADER
  117. header.CopyTo( data, 0 );
  118.  
  119. //BITMAPINFOHEADER
  120. header = new byte[Marshal.SizeOf( bi )];
  121. IntPtr pHeader = LocalAlloc( Gptr, ( uint )Marshal.SizeOf( bi ) );
  122. Marshal.StructureToPtr( biNew, pHeader, false );
  123. Marshal.Copy( pHeader, header, 0, Marshal.SizeOf( bi ) );
  124. LocalFree( pHeader );
  125.  
  126. header.CopyTo( data, HdrSize );
  127.  
  128. //set black color as second color from color table
  129. byte[] colors = new byte[10];
  130. colors[4] = 255;
  131. colors[5] = 255;
  132. colors[6] = 255;
  133.  
  134. colors.CopyTo( data, ( int )bfh.bfOffBits - ( int )colorSize );
  135.  
  136. //Bitmap bits
  137. RealBits.CopyTo( data, ( int )bfh.bfOffBits );
  138.  
  139. DeleteObject( SelectObject( hMemDc, hOldBitmap ) );
  140. DeleteDC( hMemDc );
  141.  
  142. return data;
  143. }
  144.  
  145. [DllImport( "coredll.dll" )]
  146. public static extern bool DeleteObject( IntPtr hObject );
  147.  
  148. [DllImport( "coredll.dll" )]
  149. public static extern int InvalidateRect( IntPtr hwnd, IntPtr rect, int bErase );
  150.  
  151. [DllImport( "coredll.dll" )]
  152. public static extern IntPtr GetDC( IntPtr hwnd );
  153.  
  154. [DllImport( "coredll.dll" )]
  155. public static extern IntPtr CreateCompatibleDC( IntPtr hdc );
  156.  
  157. [DllImport( "coredll.dll" )]
  158. public static extern int ReleaseDC( IntPtr hwnd, IntPtr hdc );
  159.  
  160. [DllImport( "coredll.dll" )]
  161. public static extern int DeleteDC( IntPtr hdc );
  162.  
  163. [DllImport( "coredll.dll" )]
  164. public static extern IntPtr SelectObject( IntPtr hdc, IntPtr hgdiobj );
  165.  
  166. [DllImport( "coredll.dll" )]
  167. public static extern int BitBlt( IntPtr hdcDst, int xDst, int yDst, int w, int h, IntPtr hdcSrc, int xSrc, int ySrc, int rop );
  168.  
  169. [DllImport( "coredll.dll" )]
  170. private static extern IntPtr LocalAlloc( uint flags, uint cb );
  171.  
  172. [DllImport( "coredll.dll" )]
  173. private static extern IntPtr LocalFree( IntPtr hMem );
  174.  
  175. [DllImport( "coredll.dll" )]
  176. private static extern IntPtr CreateDIBSection( IntPtr hdc, IntPtr hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset );
Add Comment
Please, Sign In to add comment