/// <summary>
        /// Returns a Graphics object representing a buffer.
        /// </summary>
        private Graphics CreateBuffer(IntPtr src, int offsetX, int offsetY, int width, int height)
        {
            // Create the compat DC.
            _busy = BufferBusyDisposing;
            DisposeDC();
            _busy     = BufferBusyPainting;
            _compatDC = UnsafeNativeMethods.CreateCompatibleDC(new HandleRef(null, src));

            // Recreate the bitmap if necessary.
            if (width > _bufferSize.Width || height > _bufferSize.Height)
            {
                int optWidth  = Math.Max(width, _bufferSize.Width);
                int optHeight = Math.Max(height, _bufferSize.Height);

                _busy = BufferBusyDisposing;
                DisposeBitmap();
                _busy = BufferBusyPainting;

                IntPtr pvbits = IntPtr.Zero;
                _dib        = CreateCompatibleDIB(src, IntPtr.Zero, optWidth, optHeight, ref pvbits);
                _bufferSize = new Size(optWidth, optHeight);
            }

            // Select the bitmap.
            _oldBitmap = SafeNativeMethods.SelectObject(new HandleRef(this, _compatDC), new HandleRef(this, _dib));

            // Create compat graphics.
            _compatGraphics = Graphics.FromHdcInternal(_compatDC);
            _compatGraphics.TranslateTransform(-_targetLoc.X, -_targetLoc.Y);
            _virtualSize = new Size(width, height);

            return(_compatGraphics);
        }
        private Graphics CreateBuffer(IntPtr src, int offsetX, int offsetY, int width, int height)
        {
            //create the compat DC
            busy = BUFFER_BUSY_DISPOSING;
            DisposeDC();
            busy     = BUFFER_BUSY_PAINTING;
            compatDC = UnsafeNativeMethods.CreateCompatibleDC(new HandleRef(null, src));

            //recreate the bitmap if necessary
            if (width > bufferSize.Width || height > bufferSize.Height)
            {
                Debug.WriteLineIf(DoubleBuffering.TraceInfo, "allocating new bitmap: " + width + " x " + height);
                int optWidth  = Math.Max(width, bufferSize.Width);
                int optHeight = Math.Max(height, bufferSize.Height);

                busy = BUFFER_BUSY_DISPOSING;
                DisposeBitmap();
                busy = BUFFER_BUSY_PAINTING;

                Debug.WriteLineIf(DoubleBuffering.TraceInfo, "    new size         : " + optWidth + " x " + optHeight);
                IntPtr pvbits = IntPtr.Zero;
                dib        = CreateCompatibleDIB(src, IntPtr.Zero, optWidth, optHeight, ref pvbits);
                bufferSize = new Size(optWidth, optHeight);
            }

            //select the bitmap
            oldBitmap = SafeNativeMethods.SelectObject(new HandleRef(this, compatDC), new HandleRef(this, dib));

            //create compat graphics
            Debug.WriteLineIf(DoubleBuffering.TraceInfo, "    Create compatGraphics");
            compatGraphics = Graphics.FromHdcInternal(compatDC);
            compatGraphics.TranslateTransform(-targetLoc.X, -targetLoc.Y);
            virtualSize = new Size(width, height);

            return(compatGraphics);
        }