在WPF中打印预览类似于WIndows Forms

时间:2013-02-27 14:41:18

标签: c# wpf printing preview

我没有找到解决方案,不是在stackoverflow或google中。 我已经在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);
    }
}

我要做什么? 我打印一个窗口(我可以在WPF中使用PrintDialog.PrintVisual进行打印),但我希望将其显示为带有动态纸张大小的预览。

在WPF中是否容易做类似的事情?

0 个答案:

没有答案