如何从 FrameworkElement 创建高分辨率图像?

How to Create High-Resolution Image from a FrameworkElement?

我有一个 FrameworkElement,我想将它保存为高度分辨率图像 (Jpeg)。我尝试了以下代码,但结果不是我所希望的。当我用 A4 作为纸张尺寸打印时,我得到了一张分辨率很差的图像。

        // ...
        FrameworkElement element = this.Content;           
        element.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
        element.Arrange(new Rect(element.DesiredSize));
        element.UpdateLayout();

        // i used element.ActulaHeight and ActualWidth but not works.
        RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.Width, (int)element.Height, GPFReporstPageSizeExtension.DPI, GPFReporstPageSizeExtension.DPI, System.Windows.Media.PixelFormats.Pbgra32);
        bitmap.Render(element);
        // ....

最后我解决了我的问题,我在我的博客“gamadev 网站”中发布了完整的解决方案: Saving FrameworkElement as Image

    FrameworkElement element = myControl.Content;
// you can set the size as you need.
Size theTargetSize = new Size(1500,2000)
element.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(theTargetSize ));
// to affect the changes in the UI, you must call this method at the end to apply the new changes
element.UpdateLayout();

double dpiScale = 300.0 / 96;

double dpiX = 300.0;
double dpiY = 300.0;
RenderTargetBitmap bmp = new RenderTargetBitmap(Convert.ToInt32(
(theTargetSize .Width) * dpiScale),
Convert.ToInt32((theTargetSize .Height) * dpiScale),
dpiX, dpiY, PixelFormats.Pbgra32);

bmp.Render(element);

element.Measure(new System.Windows.Size());
element.Arrange(new Rect());
element.UpdateLayout();

System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
MemoryStream myStream = new MemoryStream();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(this.CreateRenderTargetBitmap()));
encoder.Save(myStream);
var img = System.Drawing.Bitmap.FromStream(myStream);

Bitmap bmp = new Bitmap((int)theTargetSize .Width, (int)theTargetSize .Height);

var g = Graphics.FromImage(bmp);
g.Clear(System.Drawing.Color.White);

g.DrawImage(this.GetPageAsImage(), (int)this.Margin.Left, (int)this.Margin.Top);
fileName = @”D:\myImage.png”;
bmp.Save(fileName);