1

I didn't find a solution, not in stackoverflow nor in google. I already did that in WindowsForms:

static class PrintHandler
{
    private static Bitmap memoryImage;
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    private static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);

    public static void Print(Form board)
    {
        PrintPreviewDialog pPreview = new PrintPreviewDialog();
        PrintDocument pDoc = new PrintDocument();

        pPreview.Document = pDoc;
        captureScreen(board);

        pDoc.DefaultPageSettings.PaperSize = new PaperSize("Paper", board.ClientRectangle.Width, board.ClientRectangle.Height);

        pDoc.PrintPage += new PrintPageEventHandler(pDoc_PrintPage);

        pPreview.Show(board);
    }

    static void  pDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

    private static void captureScreen(Form board)
    {
        Graphics mygraphics = board.CreateGraphics();
        Size s = board.Size;
        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        IntPtr dc1 = mygraphics.GetHdc();
        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, board.ClientRectangle.Width, board.ClientRectangle.Height, dc1, 0, 24, 13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);
    }
}

What I wat to do? I print a Window (which I can do in WPF with PrintDialog.PrintVisual), but I want to show it as preview with dynamic Paper-size.

Is there an easy to do something similiar in WPF?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
  • Can you embed your WinForm in your WPF? This will be easy – mlemay Feb 27 '13 at 14:46
  • If you can use a flowdocument, you can convert to XPS. There is a related SO post here: http://stackoverflow.com/questions/2322064/how-can-i-produce-a-print-preview-of-a-flowdocument-in-a-wpf-application which provides code to do this. – Jay Feb 27 '13 at 19:43
  • Checking this out, thanks a lot. Since I'm just learning wpf, i dont want convert anything from winfowms. – Matthias Müller Feb 28 '13 at 09:21
  • Did I see this right, this works only for .net 4.5 right? – Matthias Müller Feb 28 '13 at 09:38

0 Answers0