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