与装饰器复制UI元素 [英] Copy UI element with adorner

查看:191
本文介绍了与装饰器复制UI元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在各种尺寸的考虑UI元素(WPF)的屏幕截图和我能够做到这一点使用RenderTargetBitmap,但的UIElement 里面有一个装饰器部分不来,而采取的副本。我应该做些什么来实现这一目标。任何引用或代码段?

I am working on taking screenshot of UI Element (WPF) in various size and i'm able to achieve this using "RenderTargetBitmap. But the UIElement which has an Adorner part is not coming while take copy. What should i do to achieve this. Any reference or code snippet?

推荐答案

据我所知,元素没有自己的装饰器直接引用。装饰器不通过的 AdornedElement 的,所以您可以搜索分配给你的元素这样的装饰器:

As far as I know, elements don't have direct references to their adorners. Adorners do reference their element through AdornedElement though, so you could search for adorners assigned to your element like this:

var layer = AdornerLayer.GetAdornerLayer(element);
var adorners = layer.GetVisualChildren().Cast<Adorner>().Where(a => a.AdornedElement == element);

下面 GetVisualChildren 是被定义为一个扩展方法:

Here GetVisualChildren is an extension method which is defined as:

public static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject current) {
    return Enumerable.Range(0, VisualTreeHelper.GetChildrenCount(current)).Select(i => VisualTreeHelper.GetChild(current, i));
}



装饰器的大小似乎包括佐餐元件的尺寸(虽然我不知道这是总是如此),所以如果只有一个装饰器,那是你的截图大小。如果有多个装饰器,你需要找到每个绑定(左,上,右,下)的最大计算的截图区域。

The size of the adorner seems to include the size of the adorned element (although I'm not sure if this is always the case), so if there is only one adorner, that is your screenshot size. If there is more than one adorner, you would need to find the maximum for each bound (left, top, right, bottom) to calculate the screenshot region.

您需要捕获 AdornerDecorator 其中包含被装饰的元素和 AdornerLayer 在上面的代码)。这将是该层的视觉父:

You will need to capture the AdornerDecorator which contains both the elements being adorned and the AdornerLayer (layer in the above code). That would be the visual parent of the layer:

var container = VisualTreeHelper.GetParent(layer) as Visual;



一旦你的容器,你可以用 RenderTargetBitmap 并作物的截图区域。

有关的截图区域,你需要的元素界限相对于容器。首先,获得了非亲属范围:

For the screenshot region, you need the element bounds relative to the container. First, get the non-relative bounds:

var elementBounds = element.RenderTransform.TransformBounds(new Rect(element.RenderSize));



然后得到相对于容器的边界:

Then get those bounds relative to the container:

var relativeElementBounds = element.TransformToAncestor(container).TransformBounds(elementBounds);

正如我上面提到的,你需要为元素做到这一点,以及它的每一个装饰器的和最大边界组合成一个矩形最终这只是大到足以容纳所有的人。

As I mentioned above, you will need to do this for the element as well as each of its adorners and combine the maximum bounds into one final Rect which is just large enough to contain all of them.

最后,使用的 CroppedBitmap 得到的裁剪版的 RenderTargetBitmap

Finally, use CroppedBitmap to get a cropped version of the RenderTargetBitmap:

var croppedBitmap = new CroppedBitmap(renderTargetBitmap, new Int32Rect(left, top, width, height));



CroppedBitmap RenderTargetBitmap 无论从的BitmapSource 继承,所以你应该能够将其保存以同样的方式。

CroppedBitmap and RenderTargetBitmap both inherit from BitmapSource, so you should be able to save it the same way.

这篇关于与装饰器复制UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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