private Graphics CreateBuffer(IntPtr src, int offsetX, int offsetY, int width, int height) { //NativeMethods.POINT pVp = new NativeMethods.POINT(); if (width <= bufferWidth && height <= bufferHeight && compatDC != IntPtr.Zero) { virtualWidth = width; virtualHeight = height; Debug.WriteLineIf(DoubleBuffering.TraceInfo, "Reusing compatible DC"); if (compatGraphics != null) { Debug.WriteLineIf(DoubleBuffering.TraceInfo, " Disposing compatGraphics"); compatGraphics.Dispose(); compatGraphics = null; } //SafeNativeMethods.SetViewportOrgEx(compatDC, offsetX, offsetY, pVp); Debug.WriteLineIf(DoubleBuffering.TraceInfo, " Create compatGraphics"); compatGraphics = Graphics.FromHdcInternal(compatDC); compatGraphics.TranslateTransform(-targetX, -targetY); return(compatGraphics); } int optWidth = bufferWidth; int optHeight = bufferHeight; Dispose(); Debug.WriteLineIf(DoubleBuffering.TraceInfo, "allocating new buffer: " + width + " x " + height); Debug.WriteLineIf(DoubleBuffering.TraceInfo, " old size : " + optWidth + " x " + optHeight); optWidth = Math.Max(width, optWidth); optHeight = Math.Max(height, optHeight); Debug.WriteLineIf(DoubleBuffering.TraceInfo, " new size : " + optWidth + " x " + optHeight); IntPtr pvbits = IntPtr.Zero; dib = CreateCompatibleDIB(src, IntPtr.Zero, optWidth, optHeight, ref pvbits); compatDC = UnsafeNativeMethods.CreateCompatibleDC(new HandleRef(null, src)); oldBitmap = SafeNativeMethods.SelectObject(new HandleRef(this, compatDC), new HandleRef(this, dib)); //SafeNativeMethods.SetViewportOrgEx(compatDC, offsetX, offsetY, pVp); Debug.WriteLineIf(DoubleBuffering.TraceInfo, " Create compatGraphics"); compatGraphics = Graphics.FromHdcInternal(compatDC); compatGraphics.TranslateTransform(-targetX, -targetY); bufferWidth = optWidth; bufferHeight = optHeight; virtualWidth = width; virtualHeight = height; return(compatGraphics); }