简体   繁体   English

如何将WPF元素另存为不同大小的图像?

[英]How to save WPF element as an image in different sizes?

In my application, I have a chart (WPF Toolkit) in normal resolution/size (for example 640x480). 在我的应用程序中,我有一个正常分辨率/大小(例如640x480)的图表(WPF工具包)。 I want to export this chart to an image in different resolutions (for example 800x600 or greater). 我想将此图表导出到不同分辨率(例如800x600或更高)的图像。

Currently, I use these lines of codes: 目前,我使用以下几行代码:

var path = String.Format(@"C:\Temp\chart-{0}.bmp", DateTime.Now.Ticks);
var renderBitmap = new RenderTargetBitmap((int)xamChart.ActualWidth,
                                          (int)xamChart.ActualHeight,
                                          96d,
                                          96d,
                                          PixelFormats.Pbgra32);
renderBitmap.Render(xamChart);

// Create a file stream for saving image
using (var outStream = new FileStream(path, FileMode.Create))
{
   // Use png encoder for our data
   var encoder = new BmpBitmapEncoder();

   // push the rendered bitmap to it
   encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

   // save the data to the stream
   encoder.Save(outStream);
}

It works very well, but if I want to save in greater resolution, I need to change size of the chart. 它工作得很好,但是如果要保存更高的分辨率,则需要更改图表的大小。 These changes of sizes are visible on my form and are not acceptable. 这些尺寸的变化在我的表单上可见,是不可接受的。 Is there a way to keep the original size of the element and save it as an image in different sizes? 有没有办法保持元素的原始大小并将其另存为不同大小的图像?

PS. PS。 I don't want save the chart to an image in its original size and in then change the image size, because I lose image quality. 我不想将图表保存为原始大小的图像,然后更改图像大小,因为这样会降低图像质量。

PPS. PPS。 Second solution: Maybe someone knows how to export a WPF element to vector graphics? 第二种解决方案:也许有人知道如何将WPF元素导出到矢量图形?

我根本没有尝试过,但是可以通过增加DPI( RenderTargetBitmap第四和第五个参数)来完成您要求的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM