-1

I'm using PrintDocument to print a receipt in for a POS system. I'm using the PrintPage event handler's graphics object to do the printing. The application is written using WPF with .NET 7.

It would be good if I could show a preview in the WPF application before printing. Is there any possibility to display a System.Drawing.Graphics object in a user control? If I could, I can re-use the same logic.

Thanuja Dilhan
  • 137
  • 2
  • 10

1 Answers1

0

You cannot display a System.Drawing.Graphics object directly. What you can do is draw to a bitmap with Graphics.FromImage and display the bitmap in wpf.

Something like:

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));
JonasH
  • 28,608
  • 2
  • 10
  • 23