/// <summary> /// Draws the specified <see cref="BitmapEx"/> object at the specified location and with the original size. /// </summary> /// <param name="image"><see cref="BitmapEx"/> object to draw.</param> /// <param name="x">x-coordinate of the upper-left corner of the drawn image.</param> /// <param name="y">y-coordinate of the upper-top corner of the drawn image.</param> public void DrawImage(BitmapEx image, int x, int y) { IntPtr hdcMem = GDIPlus.CreateCompatibleDC(hDC); IntPtr hOldSel = GDIPlus.SelectObject(hdcMem, image.hBitmap); GDIPlus.BitBlt(hDC, x, y, image.Width, image.Height, hdcMem, 0, 0, GDIPlus.SRCCOPY); GDIPlus.SelectObject(hdcMem, hOldSel); GDIPlus.DeleteDC(hdcMem); }
/// <summary> /// Draws the specified portion of the specified System.Drawing.Image object at the specified location and with the specified size. /// </summary> /// <param name="image"><see cref="OpenNETCF.Drawing.BitmapEx"/> object to draw.</param> /// <param name="destRect"><see cref="Rectangle"/> structure that specifies the location and size of the drawn image. /// The image is scaled to fit the rectangle.</param> /// <param name="srcRect"><see cref="Rectangle"/> structure that specifies the portion of the image object to draw.</param> public void DrawImage(BitmapEx image, Rectangle destRect, Rectangle srcRect) { IntPtr hdcMem = GDIPlus.CreateCompatibleDC(hDC); IntPtr hOldSel = GDIPlus.SelectObject(hdcMem, image.hBitmap); //GDIPlus.BitBlt(hDC, 0, 0, image.Width, image.Height, hdcMem, 0, 0, GDIPlus.SRCCOPY); GDIPlus.StretchBlt(hDC, destRect.Left, destRect.Top, destRect.Width, destRect.Height, hdcMem, srcRect.Left, srcRect.Top, srcRect.Width, srcRect.Height, GDIPlus.SRCCOPY); GDIPlus.SelectObject(hdcMem, hOldSel); GDIPlus.DeleteDC(hdcMem); }
public static IntPtr BitmapLockBits(BitmapEx bitmap, int flags, int format, BitmapData bitmapData) { IntPtr hDC = GetDC(IntPtr.Zero); BITMAPINFOHEADER bi = new BITMAPINFOHEADER(); bi.biSize = (uint)Marshal.SizeOf(bi); //For now they are all converted to 24 bits bi.biBitCount = 24; // Creating RGB bitmap. The following three members don't matter //bi.biBitCount = (ushort)bitmap.bitsPixel; bi.biClrUsed = 0; bi.biClrImportant = 0; if (bi.biBitCount == 16 || bi.biBitCount == 32) { bi.biCompression = BI_BITFIELDS; } else { bi.biCompression = BI_RGB; } //bi.biCompression = 0; bi.biHeight = bitmap.Height; bi.biWidth = bitmap.Width; bi.biPlanes = 1; int cb = (int)(bi.biHeight * bi.biWidth * bi.biBitCount / 8); //8 is bits per byte //bi.biSizeImage = (uint)cb; bi.biSizeImage = (uint)(BytesPerLine(bi.biBitCount, bitmap.Width) * bitmap.Height); bi.biXPelsPerMeter = 0xb12; // 72 ppi, 96 would work well too bi.biYPelsPerMeter = 0xb12; // 72 ppi IntPtr pBits = IntPtr.Zero; //Allocate memory for bitmap bits IntPtr pBI = LocalAlloc(GPTR, (int)bi.biSize); // Not sure if this needed - simply trying to keep marshaller happy Marshal.StructureToPtr(bi, pBI, false); //This will return IntPtr to actual DIB bits in pBits IntPtr hBmp = CreateDIBSection(hDC, pBI, 0, ref pBits, IntPtr.Zero, 0); //Marshall back - now we have BITMAPINFOHEADER correctly filled in Marshal.PtrToStructure(pBI, bi); BITMAPINFOHEADER biNew = (BITMAPINFOHEADER)Marshal.PtrToStructure(pBI, typeof(BITMAPINFOHEADER)); IntPtr hMemDC = CreateCompatibleDC(hDC); IntPtr hTargetDC = CreateCompatibleDC(hDC); //Usual stuff IntPtr hOldBitmap1 = SelectObject(hMemDC, bitmap.hBitmap); IntPtr hOldBitmap2 = SelectObject(hTargetDC, hBmp); //Grab bitmap int nRet = BitBlt(hTargetDC, 0, 0, bitmap.Width, bitmap.Height, hMemDC, 0, 0, SRCCOPY); bitmapData.Height = bitmap.Height; bitmapData.Width = bitmap.Width; bitmapData.Scan0 = pBits; bitmapData.Stride = BytesPerLine(biNew.biBitCount, bitmap.Width); //Restore SelectObject(hMemDC, hOldBitmap1); SelectObject(hTargetDC, hOldBitmap2); //Clean up DeleteDC(hMemDC); DeleteDC(hTargetDC); return(hBmp); }