private byte[] GetBitmapDataRaw( Bitmap bitmap, out TinyResourceFile.CLR_GFX_BitmapDescription bitmapDescription, byte type ) { bitmapDescription = new TinyResourceFile.CLR_GFX_BitmapDescription( (ushort)bitmap.Width, (ushort)bitmap.Height, 0, 1, type); MemoryStream stream = new MemoryStream(); bitmap.Save( stream, bitmap.RawFormat ); stream.Capacity = (int)stream.Length; return stream.GetBuffer(); }
private byte[] GetBitmapDataBmp( Bitmap bitmap, out TinyResourceFile.CLR_GFX_BitmapDescription bitmapDescription ) { //issue warning for formats that we lose information? //other formats that we need to support?? byte bitsPerPixel = 24; BitmapData bitmapData = null; Rectangle rect = new Rectangle( 0, 0, bitmap.Width, bitmap.Height ); PixelFormat formatDst = bitmap.PixelFormat; byte[] data = null; switch(bitmap.PixelFormat) { case PixelFormat.Format1bppIndexed: bitsPerPixel = 1; formatDst = PixelFormat.Format1bppIndexed; break; // Anything more than 16bpp will fall through to 16bpp case PixelFormat.Format8bppIndexed: case PixelFormat.Format24bppRgb: case PixelFormat.Format32bppRgb: case PixelFormat.Format48bppRgb: case PixelFormat.Format16bppRgb555: case PixelFormat.Format16bppRgb565: bitsPerPixel = 16; formatDst = PixelFormat.Format16bppRgb565; break; default: throw new NotSupportedException( string.Format( "PixelFormat of '{0}' resource not supported", this.Name ) ); } //turn bitmap data into a form we can use. if(formatDst != bitmap.PixelFormat) { bitmap = bitmap.Clone( rect, formatDst ); } try { bitmapData = bitmap.LockBits( rect, ImageLockMode.ReadOnly, formatDst ); IntPtr p = bitmapData.Scan0; data = new byte[bitmapData.Stride * bitmap.Height]; System.Runtime.InteropServices.Marshal.Copy( bitmapData.Scan0, data, 0, data.Length ); if(bitsPerPixel == 1) { //special case for 1pp with index 0 equals white???!!!??? if(bitmap.Palette.Entries[0].GetBrightness() < 0.5) { for(int i = 0; i < data.Length; i++) { data[i] = (byte)~data[i]; } } //special case for 1pp need to flip orientation?? //for some stupid reason, 1bpp is flipped compared to windows!! Adjust1bppOrientation( data ); } } finally { if(bitmapData != null) { bitmap.UnlockBits( bitmapData ); } } bitmapDescription = new TinyResourceFile.CLR_GFX_BitmapDescription( (ushort)bitmap.Width, (ushort)bitmap.Height, 0, bitsPerPixel, TinyResourceFile.CLR_GFX_BitmapDescription.c_TypeBitmap ); if(bitsPerPixel == 1) { //test compression; Compress1bpp( bitmapDescription, ref data ); } return data; }