【发布时间】:2023-01-23 20:46:35
【问题描述】:
我正在使用 PrintDocument 为 POS 系统打印收据。我正在使用 PrintPage 事件处理程序的图形对象进行打印。该应用程序是使用 WPF 和 .NET 7 编写的。
如果我能在打印前在 WPF 应用程序中显示预览就好了。是否有可能在用户控件中显示 System.Drawing.Graphics 对象?如果可以的话,我可以重复使用相同的逻辑。
【问题讨论】:
我正在使用 PrintDocument 为 POS 系统打印收据。我正在使用 PrintPage 事件处理程序的图形对象进行打印。该应用程序是使用 WPF 和 .NET 7 编写的。
如果我能在打印前在 WPF 应用程序中显示预览就好了。是否有可能在用户控件中显示 System.Drawing.Graphics 对象?如果可以的话,我可以重复使用相同的逻辑。
【问题讨论】:
您不能直接显示 System.Drawing.Graphics 对象。您可以做的是使用Graphics.FromImage 和display the bitmap in wpf 绘制位图。
就像是:
var bitmap = new Bitmap(512, 512);
using(var g = Graphics.FromImage(bitmap)){
// Do drawing
}
var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(), // you will need to delete this hbitmap
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(512, 512));
【讨论】: