获取使用的VisualBrush一个WPF区System.Drawing.Bitmap [英] Get System.Drawing.Bitmap of a WPF Area using VisualBrush

查看:259
本文介绍了获取使用的VisualBrush一个WPF区System.Drawing.Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的关键是,我需要转换为System.Drawing.Bitmap(.Net框架2.0)得到一个WPF网格其内容的单个帧。

The point is, that I need to convert to a System.Drawing.Bitmap (.Net Framework 2.0) to get a single frame of an WPF Grid with its content.

我读的VisualBrush DrawingBrush ,但我不能想象它应该如何工作。

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

我可以在任何WPF 的BitmapSource 转换成我的系统。 Drawing.Bitmap 成功。但如何获得的BitmapSource 从我的网格?

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

感谢

推荐答案

要转换视觉的BitmapSource 您可以使用< A HREF =htt​​p://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx> RenderTargetBitmap 的VisualBrush DrawingVisual

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;
}   

这篇关于获取使用的VisualBrush一个WPF区System.Drawing.Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆