// parses a BITMAPFILEHEADER and BITMAPINFOHEADER to get // the offset of the image bits, the size of the image and // if it's a transparent bitmap. private static void GetBitmapInfo(byte[] data, out uint BitsOffset, out uint ImageSize, out bool IsTransparent) { BitsOffset = 0; ImageSize = 0; IsTransparent = false; using (BinaryReader reader = new BinaryReader(new MemoryStream(data))) { BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER(); BITMAPINFOHEADER infoHeader = new BITMAPINFOHEADER(); if (!fileHeader.Read(reader)) { return; } if (!infoHeader.Read(reader)) { return; } BitsOffset = fileHeader.bfOffBits; ImageSize = (uint)(System.Math.Abs(infoHeader.biWidth) * System.Math.Abs(infoHeader.biHeight)); IsTransparent = infoHeader.IsTransparent; } }
// Display a bitmap at a given location. public void BitBltBmp(int x, int y, byte[] buffer) { BITMAPFILEHEADER bfh; BITMAPINFOHEADER bih; int lDelta; int cbScanLine; int used; int offset = 0; bfh = BITMAPFILEHEADER.Read(buffer, offset, out used); bih = BITMAPINFOHEADER.Read(buffer, used, out used); if (bih.biWidth == 0 || bih.biHeight == 0) { return; } if (x < 0) { x = SCREEN_WIDTH + x - bih.biWidth; } if (y < 0) { y = SCREEN_HEIGHT + y - bih.biHeight; } if (x < 0 || x + bih.biWidth > SCREEN_WIDTH || y < 0 || y + bih.biHeight > SCREEN_HEIGHT) { throw new OverflowException("Draw bounds invalid."); } offset = offset + bfh.bfOffBits; used = offset; // // Make sure this is a 1bpp or 4bpp bitmap. // if ((bih.biBitCount * bih.biPlanes) <= 4 || bih.biCompression != 0) { cbScanLine = (((bih.biWidth * bih.biBitCount) + 31) & ~31) / 8; if (bih.biHeight < 0) { // top down bitmap lDelta = cbScanLine; bih.biHeight = -bih.biHeight; } else { // bottom up bitmap offset += cbScanLine * (bih.biHeight - 1); lDelta = -cbScanLine; } if (used + cbScanLine * bih.biHeight > buffer.Length) { DebugStub.Print("{0} + {1} * {2} = {3} > {4}\n", __arglist( used, cbScanLine, bih.biHeight, used + cbScanLine * bih.biHeight, buffer.Length); throw new OverflowException("Bitmap invalid."); } if (bih.biBitCount == 1) { BitBlt1(x, y, bih.biWidth, bih.biHeight, buffer, offset, lDelta, 15); } else if (bih.biBitCount == 4) { BitBlt4(x, y, bih.biWidth, bih.biHeight, buffer, offset, lDelta); } } else { // // We don't support this type of bitmap. // DebugStub.Print("((bih.biBitCount * bih.biPlanes) not supported"); } }
// parses a BITMAPFILEHEADER and BITMAPINFOHEADER to get // the offset of the image bits, the size of the image and // if it's a transparent bitmap. private static void GetBitmapInfo(byte[] data, out uint BitsOffset, out uint ImageSize, out bool IsTransparent) { BitsOffset = 0; ImageSize = 0; IsTransparent = false; using(BinaryReader reader = new BinaryReader(new MemoryStream(data))) { BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER(); BITMAPINFOHEADER infoHeader = new BITMAPINFOHEADER(); if(!fileHeader.Read(reader)) return; if(!infoHeader.Read(reader)) return; BitsOffset = fileHeader.bfOffBits; ImageSize = (uint)(System.Math.Abs(infoHeader.biWidth) * System.Math.Abs(infoHeader.biHeight)); IsTransparent = infoHeader.IsTransparent; } }