C#Winforms Printing

时间:2010-01-30 09:47:43

标签: c# winforms printing

我有一个winform,我想打印winform上的所有可用数据,假设我有完整的标签,如何打印它。

3 个答案:

答案 0 :(得分:3)

以下代码示例来自How to: Print a Windows Form (MSDN),位于标题为"Print a Winform/visual element"的SO问题中,该问题位于search results for "winforms printing"的第一页:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;

public class Form1 : Form
{
    private Button printButton = new Button();
    private PrintDocument printDocument1 = new PrintDocument();

    public Form1()
    {
        printButton.Text = "Print Form";
        printButton.Click += printButton_Click;
        printDocument1.PrintPage += printDocument1_PrintPage;
        this.Controls.Add(printButton);
    }

    void printButton_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
    }

    Bitmap memoryImage;

    private void CaptureScreen()
    {
        Graphics myGraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, s);
    }

    private void printDocument1_PrintPage(System.Object sender,  
           System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }
}

答案 1 :(得分:0)

    Bitmap bmp = new Bitmap(this.Size.Width, this.Size.Height);
    panel1.DrawToBitmap(bmp); //You can replace panel1 with thiswith a parent like a panel,etc.
    //now you have a bitmap just print it using a PrintDocument
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
    {
        e.Graphics.DrawImage(bmp);
    }

答案 2 :(得分:-1)

的伪

string printString = "";    
foreach(Label lbl in this.Controls){
     printString += lbl.Text + "\n";
}

Print(printString);