C# 如何在windows窗体中打印面板?

C# 如何在windows窗体中打印面板?,c#,winforms,C#,Winforms,我有windows窗体,但我想将windows窗体中的面板打印到隐藏打印按钮,这是我的代码: private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint); int

我有windows窗体,但我想将windows窗体中的面板打印到隐藏打印按钮,这是我的代码:

   private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
        int x = e.MarginBounds.X;
        int y = e.MarginBounds.Y;
        int width = image.Width;
        int height = image.Height;
        if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
        {
            width = e.MarginBounds.Width;
            height = image.Height * e.MarginBounds.Width / image.Width;
        }
        else
        {
            height = e.MarginBounds.Height;
            width = image.Width * e.MarginBounds.Height / image.Height;
        }
        System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
        e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
    }

    public void StartPrint(Stream streamToPrint, string streamType)
    {
        this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);


        this.streamToPrint = streamToPrint;
        this.streamType = streamType;
        System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
        PrintDialog1.AllowSomePages = true;
        PrintDialog1.ShowHelp = true;
        PrintDialog1.Document = printDoc;

        DialogResult result = PrintDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            printDoc.Print();
            //docToPrint.Print();
        }

    }

    private void button_Print_Certificate_Click(object sender, EventArgs e)
    {
          Graphics g1 = this.CreateGraphics();
        Image MyImage = new Bitmap(this.ClientRectangle.Width,      this.ClientRectangle.Height, g1);
        Graphics g2 = Graphics.FromImage(MyImage);
        IntPtr dc1 = g1.GetHdc();
        IntPtr dc2 = g2.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        g1.ReleaseHdc(dc1);
        g2.ReleaseHdc(dc2);
        MyImage.Save(@"D:\PrintPage.jpg", ImageFormat.Jpeg);
        FileStream fileStream = new FileStream(@"D:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
        StartPrint(fileStream, "Image");
        fileStream.Close();

        if (System.IO.File.Exists(@"D:\PrintPage.jpg"))
        {
            System.IO.File.Delete(@"D:\PrintPage.jpg");
        }
    }

为了打印特定控件,您需要将该控件用作BitBlt操作的源

因此,不要调用
this.CreateGraphics()
,而是调用
panel.CreateGraphics()
。同样,使用
panel.ClientRectangle
获取图像的宽度和高度


但是看看哪一种方法更简单。

那么问题是什么?你在标题中提到的小组如何相关?您发布的代码与您想要的有什么区别?我想在我的winform中打印面板,但此代码使用按钮打印打印打印所有表单尝试用面板对象替换
按钮中出现的所有
,我已经把这个想法变成了一个实际的答案。现在一切正常吗>