RenderTargetBitmap位置问题

时间:2015-08-25 17:44:00

标签: c# wpf .net-4.5 rendertargetbitmap

目的

使用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>

预期结果:

Expected

方法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;
}

结果:

Method 1 Result

方法2:

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

//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里面的位置和大小:

enter image description here

这里发生了什么?

2 个答案:

答案 0 :(得分:1)

修改方法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

Rendered

与另一张图片合并:

Merged

答案 1 :(得分:-1)

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

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

更改为

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

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

相关问题