将网格的所有内容导出到图像(不仅是可见区域) [英] Export all contents of grid to a image(not only visible area)

查看:74
本文介绍了将网格的所有内容导出到图像(不仅是可见区域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个通用的代码片段,将UserControl / Grid的内容导出为PNG图像。

例如:

I need to write a generic code snippet to export the contents of UserControl/Grid to a PNG image.
eg:

<Grid>
   <dockPanel>
     <Textblock>......<Textblock>
   <dockPanel>

<scrollViewer>
  <listView>
  .....
  </listview>
</scrollViewer>

<chart>....</chart>
......
<Grid>



我需要将以上UI导出到PNG。



我尝试过:




I need to export above UI to PNG.

What I have tried:

public void ExportToPNG(FrameWorkElement element, Stream stream)
{
BitmapEncoder encoder = new PngBitmapEncoder();
	var bitmap = new RenderTargetBitmap(
				(int)element.RenderSize.Width,
				(int)element.RenderSize.Height,
				96,
				96,
				PixelFormats.Default);

Rect bounds = VisualTreeHelper.GetDescendantBounds(element);

			DrawingVisual drawingVisual = new DrawingVisual();
			using (DrawingContext context = drawingVisual.RenderOpen())
			{
				VisualBrush brush = new VisualBrush(element);
				Rect rect = new Rect(0, 0, bounds.Width, bounds.Height);

				context.DrawRectangle(Brushes.White, null, rect);
				context.DrawRectangle(brush, null, rect);
			}
bitmap.Render(drawingVisual);
encoder.Frames.Add(bitmap);
encoder.Save(stream);
}





以上代码正常运行。但是,它只是导出可见区域(来自ScrollViewer)。我需要从ScrollViewer导出整个内容。



注意:某些视图可能没有ScrollViewer



任何建议或指导都将不胜感激!



谢谢!



The above code is working. But, it's just exporting the visible area(from the ScrollViewer). I need to export the whole content from the ScrollViewer.

Note: Some Views might not have ScrollViewer

Any advise or guidance would be greatly appreciated!

Thanks!

推荐答案

显示例行而不是通话是没用的。



从它的声音来看,你传递的是滚动查看器,而不是实际的内容(滚动查看器)到例程。重要的是视觉(包含在观众中)。
Showing the routine and not the "call" is useless.

From the sounds of it, you're passing the "scroll viewer", and not the actual "content" (of the scroll viewer) to the routine. It's the "visual" (contained in the viewer) that matters.


这篇关于将网格的所有内容导出到图像(不仅是可见区域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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