开发者

Get System.Drawing.Bitmap of a WPF Area using VisualBrush

开发者 https://www.devze.com 2023-03-20 09:48 出处:网络
The point is, that I need to convert to a System.Drawing.Bitmap (.Net Framework 2.0) to get a开发者_如何学C single frame of an WPF Grid with its content.

The point is, that I need to convert to a System.Drawing.Bitmap (.Net Framework 2.0) to get a开发者_如何学C single frame of an WPF Grid with its content.

I read about VisualBrush and DrawingBrush but I cannot imagine how it should work.

I can convert any WPF BitmapSource into my System.Drawing.Bitmap successfully. But how to receive the BitmapSource from my Grid?

Thanks


To convert a Visual to BitmapSource you can use RenderTargetBitmap, VisualBrush and 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;
}   
0

精彩评论

暂无评论...
验证码 换一张
取 消