【问题标题】:Possibility of showing System.Drawing.Graphics object in a WPF control在 WPF 控件中显示 System.Drawing.Graphics 对象的可能性
【发布时间】:2023-01-23 20:46:35
【问题描述】:

我正在使用 PrintDocument 为 POS 系统打印收据。我正在使用 PrintPage 事件处理程序的图形对象进行打印。该应用程序是使用 WPF 和 .NET 7 编写的。

如果我能在打印前在 WPF 应用程序中显示预览就好了。是否有可能在用户控件中显示 System.Drawing.Graphics 对象?如果可以的话,我可以重复使用相同的逻辑。

【问题讨论】:

    标签: c# .net wpf printing


    【解决方案1】:

    您不能直接显示 System.Drawing.Graphics 对象。您可以做的是使用Graphics.FromImagedisplay 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));
    

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      相关资源
      最近更新 更多