private WriteableBitmap GetSurface(PixelBuffer pixelBuffer) { if (!Monitor.IsEntered(_syncRoot)) { throw new InvalidOperationException(); } WriteableBitmap surface = pixelBuffer.Surface; if (surface == null || surface.PixelWidth != pixelBuffer.Width || surface.PixelHeight != pixelBuffer.Height) { DpiScale dpi = OffscreenGraphics.DpiScale; surface = new WriteableBitmap(pixelBuffer.Width, pixelBuffer.Height, dpi.PixelsPerInchX, dpi.PixelsPerInchY, PixelFormats.Bgra32, null); pixelBuffer.Surface = surface; } surface.Lock(); try { Marshal.Copy(pixelBuffer.DIB, 0, surface.BackBuffer, pixelBuffer.Size); surface.AddDirtyRect(pixelBuffer.GetDirtyRectangle()); pixelBuffer.ClearDirtyRectangle(); } finally { surface.Unlock(); } return(surface); }