double x = 0;
double y = 0;
GetCurrentDPI(out x, out y);
Rect bounds = VisualTreeHelper.GetDescendantBounds(element);
RenderTargetBitmap r = new RenderTargetBitmap((int)(element.ActualWidth * x / 96), (int)(element.ActualHeight * y / 96), (int)x, (int)y, PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(element);
ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}
r.Render(dv);
private static void GetCurrentDPI(out double x, out double y)
{
Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
x = 96 / m.M11;
y = 96 / m.M22;
}
WPF高DPI下的屏幕截图
本文介绍了一种在WPF应用中实现高DPI屏幕截图的方法,通过获取当前设备的DPI,并据此调整渲染目标大小,最终完成对指定元素的精确截图。
3522

被折叠的 条评论
为什么被折叠?



