目的
を使用して、コントロール (またはコントロールのセット) のスクリーンショットを撮り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;
}
結果:
方法 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);
結果:
Grid
これはと のLabel
内側の位置とサイズを無視します:
ここで何が起こっているのですか?