private void Save(Stream stream, BitmapType type, bool withBorder, int aWidth, int aHeight, Bitmap aSrcImage) { BinaryWriter writer = new BinaryWriter(stream); int width = 0; int height = aHeight; if ((aWidth % 8) == 0) { width = aWidth / 8; } else { width = (aWidth / 8) + 1; } if ((width % 4) != 0) { width += 4 - (width % 4); } BMPHEADER bmpheader = new BMPHEADER(); bmpheader.header.bfSize = bmpheader.header.bfOffBits + ((uint)(width * height)); bmpheader.info.bmiHeader.biWidth = aWidth; bmpheader.info.bmiHeader.biHeight = height; bmpheader.info.bmiHeader.biSizeImage = (uint)(width * height); bmpheader.info.bmiColor1 = new RGBQUAD(this.BackColor.B, this.BackColor.G, this.BackColor.R); bmpheader.info.bmiColor2 = new RGBQUAD(this.ForeColor.B, this.ForeColor.G, this.ForeColor.R); switch (type) { case BitmapType.Bmp1: bmpheader.Write(writer); break; case BitmapType.Binary: case BitmapType.BinaryStrict: { BINARYHEADER binaryheader = new BINARYHEADER(); binaryheader.iWidth = aWidth; binaryheader.iHeight = height; binaryheader.cBackground = new RGBQUAD(this.BackColor.B, this.BackColor.G, this.BackColor.R); binaryheader.cForeground = new RGBQUAD(this.ForeColor.B, this.ForeColor.G, this.ForeColor.R); binaryheader.Write(writer); break; } } Graphics graphics = Graphics.FromImage(aSrcImage); IntPtr hdc = graphics.GetHdc(); IntPtr ptr2 = CreateCompatibleDC(hdc); SelectObject(ptr2, aSrcImage.GetHbitmap()); IntPtr ptr3 = CreateCompatibleDC(hdc); graphics.ReleaseHdc(hdc); IntPtr zero = IntPtr.Zero; IntPtr ptr = LocalAlloc(0x40, (uint)(bmpheader.info.bmiHeader.biSize + 8)); Marshal.StructureToPtr(bmpheader.info.bmiHeader, ptr, false); new RGBQUAD(0, 0, 0).WriteToPtr(ptr, bmpheader.info.bmiHeader.biSize); new RGBQUAD(0xff, 0xff, 0xff).WriteToPtr(ptr, bmpheader.info.bmiHeader.biSize + 4); IntPtr hObj = CreateDIBSection(ptr2, ptr, 0, ref zero, IntPtr.Zero, 0); IntPtr ptr7 = SelectObject(ptr3, hObj); BitBlt(ptr3, 0, 0, bmpheader.info.bmiHeader.biWidth, bmpheader.info.bmiHeader.biHeight, ptr2, 0, 0, 0xcc0020); if (withBorder) { IntPtr ptr8 = CreatePen(0, this.m_IsQvga ? 1 : 2, 0xffffff); if ((ptr3 != IntPtr.Zero) && (ptr8 != IntPtr.Zero)) { SelectObject(ptr3, ptr8); } this.LineTo(ptr3, 0, 0, aWidth - 1, 0); this.LineTo(ptr3, aWidth - 1, 0, aWidth - 1, aHeight - 1); this.LineTo(ptr3, aWidth - 1, aHeight - 1, 0, aHeight - 1); this.LineTo(ptr3, 0, aHeight - 1, 0, 0); if (ptr8 != IntPtr.Zero) { DeleteObject(ptr8); } } byte[] destination = new byte[bmpheader.info.bmiHeader.biSizeImage]; Marshal.Copy(zero, destination, 0, (int)bmpheader.info.bmiHeader.biSizeImage); if (type == BitmapType.BinaryStrict) { this.SwapLines(destination, width, height); } writer.Write(destination); DeleteObject(SelectObject(ptr3, ptr7)); DeleteDC(ptr3); DeleteDC(ptr2); LocalFree(ptr); }
public void Load(Stream stream, BitmapType type, bool setSize) { BinaryReader reader = new BinaryReader(stream); Stream output = stream; BMPHEADER bmpheader = null; byte[] pBits = null; int width = 0; int iWidth = 0; int height = 0; switch (type) { case BitmapType.Binary: case BitmapType.BinaryStrict: { BINARYHEADER binaryheader = new BINARYHEADER(); binaryheader.Read(reader); width = binaryheader.iWidth; iWidth = binaryheader.iWidth; height = binaryheader.iHeight; if ((width % 8) != 0) { width = (width / 8) + 1; } else { width /= 8; } if ((width % 4) != 0) { width += 4 - (width % 4); } this.ForeColor = Color.FromArgb(binaryheader.cForeground.rgbRed, binaryheader.cForeground.rgbGreen, binaryheader.cForeground.rgbBlue); this.BackColor = Color.FromArgb(binaryheader.cBackground.rgbRed, binaryheader.cBackground.rgbGreen, binaryheader.cBackground.rgbBlue); output = new MemoryStream(); BinaryWriter writer = new BinaryWriter(output); bmpheader = new BMPHEADER(); bmpheader.header.bfSize = bmpheader.header.bfOffBits + ((uint) (width * height)); bmpheader.info.bmiHeader.biWidth = iWidth; bmpheader.info.bmiHeader.biHeight = height; bmpheader.info.bmiHeader.biSizeImage = (uint) (width * height); bmpheader.info.bmiColor1 = new RGBQUAD(this.BackColor.B, this.BackColor.G, this.BackColor.R); bmpheader.info.bmiColor2 = new RGBQUAD(this.ForeColor.B, this.ForeColor.G, this.ForeColor.R); bmpheader.Write(writer); long position = reader.BaseStream.Position; pBits = reader.ReadBytes((int) (reader.BaseStream.Length - reader.BaseStream.Position)); if (type == BitmapType.BinaryStrict) { this.SwapLines(pBits, width, height); } writer.Write(pBits); reader.BaseStream.Position = position; break; } default: bmpheader = new BMPHEADER(); bmpheader.Read(reader); width = bmpheader.info.bmiHeader.biWidth; iWidth = bmpheader.info.bmiHeader.biWidth; height = bmpheader.info.bmiHeader.biHeight; if ((width % 8) == 0) { width /= 8; } else { width = (width / 8) + 1; } if ((width % 4) != 0) { width += 4 - (width % 4); } this.BackColor = Color.FromArgb(bmpheader.info.bmiColor1.rgbRed, bmpheader.info.bmiColor1.rgbGreen, bmpheader.info.bmiColor1.rgbBlue); this.ForeColor = Color.FromArgb(bmpheader.info.bmiColor2.rgbRed, bmpheader.info.bmiColor2.rgbGreen, bmpheader.info.bmiColor2.rgbBlue); pBits = reader.ReadBytes((int) bmpheader.info.bmiHeader.biSizeImage); break; } if (setSize) { this.m_ImageSize.Width = iWidth; this.m_ImageSize.Height = height; if (!this.m_ImageSizeCustom) { this.Size = this.m_ImageSize; } } base.CreateGraphics(); this.CreateMemoryBitmap(); this.CreateGdiObjects(); this.CreatePenObjects(); Bitmap image = new Bitmap(this.ImageSize.Width, this.ImageSize.Height); Graphics.FromImage(image).Clear(Color.FromArgb(0xff, 0, 0xff)); Bitmap bitmap2 = new Bitmap(output); ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorKey(this.BackColor, this.BackColor); Graphics.FromImage(image).DrawImage(bitmap2, new Rectangle(0, 0, image.Width, image.Height), 0, 0, bitmap2.Width, bitmap2.Height, GraphicsUnit.Pixel, imageAttr); if (bitmap2 != null) { bitmap2.Dispose(); bitmap2 = null; } this.Clear(); IntPtr dC = GetDC(base.Handle); IntPtr hdc = CreateCompatibleDC(dC); IntPtr zero = IntPtr.Zero; IntPtr ptr = LocalAlloc(0x40, (uint) (bmpheader.info.bmiHeader.biSize + 8)); Marshal.StructureToPtr(bmpheader.info.bmiHeader, ptr, false); Marshal.WriteInt32(ptr, bmpheader.info.bmiHeader.biSize, 0); Marshal.WriteInt32(ptr, bmpheader.info.bmiHeader.biSize + 4, 0xffffff); IntPtr hObj = CreateDIBSection(dC, ptr, 0, ref zero, IntPtr.Zero, 0); if ((type == BitmapType.BinaryStrict) && (pBits == null)) { pBits = reader.ReadBytes((int) bmpheader.info.bmiHeader.biSizeImage); this.SwapLines(pBits, width, height); } Marshal.Copy(pBits, 0, zero, (int) bmpheader.info.bmiHeader.biSizeImage); pBits = null; IntPtr ptr7 = SelectObject(hdc, hObj); Graphics graphics = Graphics.FromImage(this.m_ImageDC); IntPtr hdcDest = graphics.GetHdc(); BitBlt(hdcDest, 0, 0, bmpheader.info.bmiHeader.biWidth, bmpheader.info.bmiHeader.biHeight, hdc, 0, 0, 0xcc0020); DeleteObject(SelectObject(hdc, ptr7)); DeleteDC(hdc); graphics.ReleaseHdc(hdcDest); LocalFree(ptr); this.m_isEmpty = false; if (this.m_bmp != null) { this.m_bmp.Dispose(); this.m_bmp = null; } this.m_bmp = image; this.Refresh(); }
public void Load(Stream stream, BitmapType type, bool setSize) { BinaryReader reader = new BinaryReader(stream); Stream output = stream; BMPHEADER bmpheader = null; byte[] pBits = null; int width = 0; int iWidth = 0; int height = 0; switch (type) { case BitmapType.Binary: case BitmapType.BinaryStrict: { BINARYHEADER binaryheader = new BINARYHEADER(); binaryheader.Read(reader); width = binaryheader.iWidth; iWidth = binaryheader.iWidth; height = binaryheader.iHeight; if ((width % 8) != 0) { width = (width / 8) + 1; } else { width /= 8; } if ((width % 4) != 0) { width += 4 - (width % 4); } this.ForeColor = Color.FromArgb(binaryheader.cForeground.rgbRed, binaryheader.cForeground.rgbGreen, binaryheader.cForeground.rgbBlue); this.BackColor = Color.FromArgb(binaryheader.cBackground.rgbRed, binaryheader.cBackground.rgbGreen, binaryheader.cBackground.rgbBlue); output = new MemoryStream(); BinaryWriter writer = new BinaryWriter(output); bmpheader = new BMPHEADER(); bmpheader.header.bfSize = bmpheader.header.bfOffBits + ((uint)(width * height)); bmpheader.info.bmiHeader.biWidth = iWidth; bmpheader.info.bmiHeader.biHeight = height; bmpheader.info.bmiHeader.biSizeImage = (uint)(width * height); bmpheader.info.bmiColor1 = new RGBQUAD(this.BackColor.B, this.BackColor.G, this.BackColor.R); bmpheader.info.bmiColor2 = new RGBQUAD(this.ForeColor.B, this.ForeColor.G, this.ForeColor.R); bmpheader.Write(writer); long position = reader.BaseStream.Position; pBits = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); if (type == BitmapType.BinaryStrict) { this.SwapLines(pBits, width, height); } writer.Write(pBits); reader.BaseStream.Position = position; break; } default: bmpheader = new BMPHEADER(); bmpheader.Read(reader); width = bmpheader.info.bmiHeader.biWidth; iWidth = bmpheader.info.bmiHeader.biWidth; height = bmpheader.info.bmiHeader.biHeight; if ((width % 8) == 0) { width /= 8; } else { width = (width / 8) + 1; } if ((width % 4) != 0) { width += 4 - (width % 4); } this.BackColor = Color.FromArgb(bmpheader.info.bmiColor1.rgbRed, bmpheader.info.bmiColor1.rgbGreen, bmpheader.info.bmiColor1.rgbBlue); this.ForeColor = Color.FromArgb(bmpheader.info.bmiColor2.rgbRed, bmpheader.info.bmiColor2.rgbGreen, bmpheader.info.bmiColor2.rgbBlue); pBits = reader.ReadBytes((int)bmpheader.info.bmiHeader.biSizeImage); break; } if (setSize) { this.m_ImageSize.Width = iWidth; this.m_ImageSize.Height = height; if (!this.m_ImageSizeCustom) { this.Size = this.m_ImageSize; } } base.CreateGraphics(); this.CreateMemoryBitmap(); this.CreateGdiObjects(); this.CreatePenObjects(); Bitmap image = new Bitmap(this.ImageSize.Width, this.ImageSize.Height); Graphics.FromImage(image).Clear(Color.FromArgb(0xff, 0, 0xff)); Bitmap bitmap2 = new Bitmap(output); ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorKey(this.BackColor, this.BackColor); Graphics.FromImage(image).DrawImage(bitmap2, new Rectangle(0, 0, image.Width, image.Height), 0, 0, bitmap2.Width, bitmap2.Height, GraphicsUnit.Pixel, imageAttr); if (bitmap2 != null) { bitmap2.Dispose(); bitmap2 = null; } this.Clear(); IntPtr dC = GetDC(base.Handle); IntPtr hdc = CreateCompatibleDC(dC); IntPtr zero = IntPtr.Zero; IntPtr ptr = LocalAlloc(0x40, (uint)(bmpheader.info.bmiHeader.biSize + 8)); Marshal.StructureToPtr(bmpheader.info.bmiHeader, ptr, false); Marshal.WriteInt32(ptr, bmpheader.info.bmiHeader.biSize, 0); Marshal.WriteInt32(ptr, bmpheader.info.bmiHeader.biSize + 4, 0xffffff); IntPtr hObj = CreateDIBSection(dC, ptr, 0, ref zero, IntPtr.Zero, 0); if ((type == BitmapType.BinaryStrict) && (pBits == null)) { pBits = reader.ReadBytes((int)bmpheader.info.bmiHeader.biSizeImage); this.SwapLines(pBits, width, height); } Marshal.Copy(pBits, 0, zero, (int)bmpheader.info.bmiHeader.biSizeImage); pBits = null; IntPtr ptr7 = SelectObject(hdc, hObj); Graphics graphics = Graphics.FromImage(this.m_ImageDC); IntPtr hdcDest = graphics.GetHdc(); BitBlt(hdcDest, 0, 0, bmpheader.info.bmiHeader.biWidth, bmpheader.info.bmiHeader.biHeight, hdc, 0, 0, 0xcc0020); DeleteObject(SelectObject(hdc, ptr7)); DeleteDC(hdc); graphics.ReleaseHdc(hdcDest); LocalFree(ptr); this.m_isEmpty = false; if (this.m_bmp != null) { this.m_bmp.Dispose(); this.m_bmp = null; } this.m_bmp = image; this.Refresh(); }
private void Save(Stream stream, BitmapType type, bool withBorder, int aWidth, int aHeight, Bitmap aSrcImage) { BinaryWriter writer = new BinaryWriter(stream); int width = 0; int height = aHeight; if ((aWidth % 8) == 0) { width = aWidth / 8; } else { width = (aWidth / 8) + 1; } if ((width % 4) != 0) { width += 4 - (width % 4); } BMPHEADER bmpheader = new BMPHEADER(); bmpheader.header.bfSize = bmpheader.header.bfOffBits + ((uint) (width * height)); bmpheader.info.bmiHeader.biWidth = aWidth; bmpheader.info.bmiHeader.biHeight = height; bmpheader.info.bmiHeader.biSizeImage = (uint) (width * height); bmpheader.info.bmiColor1 = new RGBQUAD(this.BackColor.B, this.BackColor.G, this.BackColor.R); bmpheader.info.bmiColor2 = new RGBQUAD(this.ForeColor.B, this.ForeColor.G, this.ForeColor.R); switch (type) { case BitmapType.Bmp1: bmpheader.Write(writer); break; case BitmapType.Binary: case BitmapType.BinaryStrict: { BINARYHEADER binaryheader = new BINARYHEADER(); binaryheader.iWidth = aWidth; binaryheader.iHeight = height; binaryheader.cBackground = new RGBQUAD(this.BackColor.B, this.BackColor.G, this.BackColor.R); binaryheader.cForeground = new RGBQUAD(this.ForeColor.B, this.ForeColor.G, this.ForeColor.R); binaryheader.Write(writer); break; } } Graphics graphics = Graphics.FromImage(aSrcImage); IntPtr hdc = graphics.GetHdc(); IntPtr ptr2 = CreateCompatibleDC(hdc); SelectObject(ptr2, aSrcImage.GetHbitmap()); IntPtr ptr3 = CreateCompatibleDC(hdc); graphics.ReleaseHdc(hdc); IntPtr zero = IntPtr.Zero; IntPtr ptr = LocalAlloc(0x40, (uint) (bmpheader.info.bmiHeader.biSize + 8)); Marshal.StructureToPtr(bmpheader.info.bmiHeader, ptr, false); new RGBQUAD(0, 0, 0).WriteToPtr(ptr, bmpheader.info.bmiHeader.biSize); new RGBQUAD(0xff, 0xff, 0xff).WriteToPtr(ptr, bmpheader.info.bmiHeader.biSize + 4); IntPtr hObj = CreateDIBSection(ptr2, ptr, 0, ref zero, IntPtr.Zero, 0); IntPtr ptr7 = SelectObject(ptr3, hObj); BitBlt(ptr3, 0, 0, bmpheader.info.bmiHeader.biWidth, bmpheader.info.bmiHeader.biHeight, ptr2, 0, 0, 0xcc0020); if (withBorder) { IntPtr ptr8 = CreatePen(0, this.m_IsQvga ? 1 : 2, 0xffffff); if ((ptr3 != IntPtr.Zero) && (ptr8 != IntPtr.Zero)) { SelectObject(ptr3, ptr8); } this.LineTo(ptr3, 0, 0, aWidth - 1, 0); this.LineTo(ptr3, aWidth - 1, 0, aWidth - 1, aHeight - 1); this.LineTo(ptr3, aWidth - 1, aHeight - 1, 0, aHeight - 1); this.LineTo(ptr3, 0, aHeight - 1, 0, 0); if (ptr8 != IntPtr.Zero) { DeleteObject(ptr8); } } byte[] destination = new byte[bmpheader.info.bmiHeader.biSizeImage]; Marshal.Copy(zero, destination, 0, (int) bmpheader.info.bmiHeader.biSizeImage); if (type == BitmapType.BinaryStrict) { this.SwapLines(destination, width, height); } writer.Write(destination); DeleteObject(SelectObject(ptr3, ptr7)); DeleteDC(ptr3); DeleteDC(ptr2); LocalFree(ptr); }