是的,我现在这样做,这是我的代码。我还必须添加一些额外的代码,以便在使用STA Apartment State的单独线程中运行它,作为所需的一些控件,不确定是否也需要这样做。
(此代码是在VB.NET(不要问),但因此它在C#中,所以它格式化由计算器更多更好的我使用一个在线转换器)。
不要改变屏幕DPI因为这是一个Windows默认(也不要问)。
private byte[] Render(FrameworkElement element)
{
const double SCREEN_DPI = 96.0;
const double TARGET_DPI = 300.0;
// Setup the bounds of the image
Rect bounds = new Rect(0, 0, element.Width, element.Height);
element.Measure(bounds.Size);
element.Arrange(bounds);
element.UpdateLayout();
// Create the target bitmap
RenderTargetBitmap rtb = new RenderTargetBitmap(
Convert.ToInt32(element.Width/SCREEN_DPI * TARGET_DPI),
Convert.ToInt32(element.Height/SCREEN_DPI * TARGET_DPI),
TARGET_DPI, TARGET_DPI, PixelFormats.Pbgra32);
// Render the visual to the bitmap.
rtb.Render(element);
System.Windows.Media.Imaging.BitmapEncoder encoder;
encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));
var ms = new System.IO.MemoryStream();
encoder.Save(ms);
ms.Flush();
ms.Seek(0, System.IO.SeekOrigin.Begin);
}
'RenderTargetBitmap'可能是一种选择,它可以让你创建'WPF'元素,并保存为'BitmapImage',你可以再打印,http://msdn.microsoft.com/en-us /library/system.windows.media.imaging.rendertargetbitmap(v=vs.110).aspx – 2014-09-04 01:30:46