繁体   English   中英

RenderTargetBitmap位置问题

[英]RenderTargetBitmap position issues

目的

使用RenderTargetBitmap拍摄一个控件(或一组控件)的屏幕快照。

资源:

<Grid Height="200" Width="500">
    <!-- Here goes any content, in my case, a Label or a Shape-->
    <Label VerticalAligment="Top" HorizontalAligment="Left" Content="Text">
</Grid>

预期结果:

预期

方法1

基本上,这是使用UIElement作为RenderTargetBitmap的源。

public static ImageSource GetRender(this UIElement source)
{
     double actualHeight = source.RenderSize.Height;
     double actualWidth = source.RenderSize.Width;

     var renderTarget = new RenderTargetBitmap((int)Math.Round(actualWidth), 
         (int)Math.Round(actualHeight), 96, 96, PixelFormats.Pbgra32);

     renderTarget.Render(source);
     return renderTarget;
}

结果:

方法1结果

方法2:

我将使用VisualBrush而不是直接将UIElement设置为RenderTargetBitmap的源。

//Same RenderTargetBitmap...

DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
    VisualBrush vb = new VisualBrush(target);
    ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}
rtb.Render(dv);

结果:

这个忽略了GridLabel内部的位置和大小:

在此处输入图片说明

这里发生了什么事?

修改方法2

我只需要获取Grid的后代的边界并仅渲染所需的部分即可。

public static ImageSource GetRender(this UIElement source, double dpi)
{
    Rect bounds = VisualTreeHelper.GetDescendantBounds(source);

    var scale = dpi / 96.0;
    var width = (bounds.Width + bounds.X)*scale;
    var height = (bounds.Height + bounds.Y)*scale;

    RenderTargetBitmap rtb = 
        new RenderTargetBitmap((int)Math.Round(width, MidpointRounding.AwayFromZero), 
        (int)Math.Round(height, MidpointRounding.AwayFromZero), 
        dpi, dpi, PixelFormats.Pbgra32);        

    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext ctx = dv.RenderOpen())
    {
        VisualBrush vb = new VisualBrush(source);
        ctx.DrawRectangle(vb, null, 
            new Rect(new Point(bounds.X, bounds.Y), new Point(width, height)));
    }

    rtb.Render(dv);
    return (ImageSource)rtb.GetAsFrozen();
}

结果:

呈现的Label / Shape

渲染

与另一张图片合并:

合并的

这是因为RenderTargetBitmap根据其父对象的坐标渲染可视对象。 其父级的边距,Padding或BorderThickness都会影响渲染的图像。 要解决此问题,您可以简单地添加一个伪父容器:如果原始的可视逻辑树像

<Grid>
    <Canvas Margin="20" />
</Grid> 

变成

<Grid>
    <Border Margin="20">
        <Canvas />
    </Border>
</Grid> 

Alignment \\ Margin的设置也应移至上级。 现在您将得到想要的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM