使用VisualBrush获取WPF区域的System.Drawing.Bitmap

Nas*_*aer 2 c# wpf drawing bitmapsource visualbrush

关键是,我需要转换为System.Drawing.Bitmap(.Net Framework 2.0)以获取WPF Grid的单帧及其内容.

我读到了VisualBrush,DrawingBrush但我无法想象它应该如何运作.

我可以将任何WPF BitmapSource转换成我的System.Drawing.Bitmap成功.但是如何BitmapSource从我的网格接收?

谢谢

Nav*_*ani 5

要将a转换VisualBitmapSource可以使用RenderTargetBitmap,VisualBrush并且DrawingVisual:

public BitmapSource ConvertToBitmapSource(UIElement element)
{
    var target = new RenderTargetBitmap((int)(element.RenderSize.Width), (int)(element.RenderSize.Height), 96, 96, PixelFormats.Pbgra32);
    var brush = new VisualBrush(element);

    var visual = new DrawingVisual();
    var drawingContext = visual.RenderOpen();


    drawingContext.DrawRectangle(brush, null, new Rect(new Point(0, 0),
        new Point(element.RenderSize.Width, element.RenderSize.Height)));

    drawingContext.Close();

    target.Render(visual);

    return target;
}   
Run Code Online (Sandbox Code Playgroud)