/// <summary> /// Draws a bitmap to a Graphics context. /// </summary> /// <param name="dst">The Graphics context to draw the bitmap on to.</param> /// <param name="dstRect">The clipping rectangle in destination coordinates.</param> /// <param name="dstMatrix">The transformation matrix to apply. This is only used to transform the upper-left corner of dstRect.</param> /// <param name="srcBitmapHandle">The handle to the bitmap obtained from Memory.AllocateBitmap().</param> /// <param name="srcWidth">The full width of the bitmap.</param> /// <param name="srcHeight">The full height of the bitmap.</param> /// <param name="srcOffsetX">The left edge of the source bitmap to draw from.</param> /// <param name="srcOffsetY">The top edge of the source bitmap to draw from.</param> public unsafe static void DrawBitmap( Graphics dst, Rectangle dstRect, Matrix dstMatrix, IntPtr srcBitmapHandle, int srcOffsetX, int srcOffsetY) { if (srcBitmapHandle == IntPtr.Zero) { throw new ArgumentNullException("srcBitmapHandle"); } Point[] points = new Point[] { dstRect.Location }; dstMatrix.TransformPoints(points); dstRect.Location = points[0]; IntPtr hdc = IntPtr.Zero; IntPtr hbitmap = IntPtr.Zero; IntPtr chdc = IntPtr.Zero; IntPtr old = IntPtr.Zero; try { hdc = dst.GetHdc(); chdc = SafeNativeMethods.CreateCompatibleDC(hdc); old = SafeNativeMethods.SelectObject(chdc, srcBitmapHandle); SafeNativeMethods.BitBlt(hdc, dstRect.Left, dstRect.Top, dstRect.Width, dstRect.Height, chdc, srcOffsetX, srcOffsetY, NativeConstants.SRCCOPY); } finally { if (old != IntPtr.Zero) { SafeNativeMethods.SelectObject(chdc, old); old = IntPtr.Zero; } if (chdc != IntPtr.Zero) { SafeNativeMethods.DeleteDC(chdc); chdc = IntPtr.Zero; } if (hdc != IntPtr.Zero) { dst.ReleaseHdc(hdc); hdc = IntPtr.Zero; } } GC.KeepAlive(dst); }
private void Dispose(bool disposing) { if (!disposed) { if (disposing) { this.graphics.Dispose(); this.graphics = null; } SafeNativeMethods.DeleteDC(this.hdc); disposed = true; } }