Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        public NullGraphics()
        {
            this.hdc = SafeNativeMethods.CreateCompatibleDC(IntPtr.Zero);

            if (this.hdc == IntPtr.Zero)
            {
                NativeMethods.ThrowOnWin32Error("CreateCompatibleDC returned NULL");
            }

            this.graphics = Graphics.FromHdc(this.hdc);
        }