[英]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>
预期结果:
基本上,这是使用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;
}
结果:
我将使用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);
结果:
这个忽略了Grid
和Label
内部的位置和大小:
这里发生了什么事?
我只需要获取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.