1

我没有找到解决方案,在 stackoverflow 和谷歌中都没有。我已经在 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 中是否有容易做类似的事情?

4

0 回答 0