如何在运行时创建 WPF UserControl 的图像 [英] How to create an image of a WPF UserControl at runtime

查看:21
本文介绍了如何在运行时创建 WPF UserControl 的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 WPF 应用程序,它有一个 Canvas,我在上面放置了 UserControls,用户可以移动和调整其大小(就像 Windows 窗口一样).现在我发现这在较旧的 PC 上可能会非常慢,这是一个问题.

I've created a WPF application which has a Canvas on which I place UserControls which are moveable and resizeable by the user (just like a Windows-Window). Now I have detected that this can be very slow on older PC's which is a problem.

作为一种解决方案,我考虑生成一个显示 UserControl 的图形并在调整大小/拖动控件时显示它,以防止 WPF 永久重新计算所有元素.唯一的问题是我不知道如何生成这个图像.

As a solution I thought about generating a graphic showing the UserControl and show this while resizing/dragging the Control, to prevent WPF from recalculating all Elements permanently. The only problem is that I have no idea how to generate this image.

在 .Net 中是否有类似的功能可以做到这一点?或者我怎么能自己做到这一点?

Is there perhaps something like a function which does this in .Net? Or how could I do this on my own?

推荐答案

您可以使用 RenderTargetBitmap,然后可以将此图像复制到剪贴板、保存到文件或用作 GUI 的一部分

You can render a WPF control to a bitmap using RenderTargetBitmap, then this image can be copied to the clipboard, saved to a file, or used as part of your GUI

查看从控制视图获取位图图像

注意这一点,当您尝试呈现的控件部分不可见(可能在滚动查看器中)时,您可能会遇到问题

Beware with this that you can hit problems when parts of the control you are trying to render are not visible (within a scroll viewer perhaps)

这篇关于如何在运行时创建 WPF UserControl 的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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