获取使用的VisualBrush一个WPF区System.Drawing.Bitmap [英] Get System.Drawing.Bitmap of a WPF Area using VisualBrush
问题描述
问题的关键是,我需要转换为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 =http://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屋!