在Windows表单C#中打印面板

4

我这里有一个情况。我需要在Windows表单中使用C#在Visual Studio 2010中创建员工卡结构。该结构可能包含具有白色背景的标签和图片框。我创建它没有问题,但我也在这个表单上放了一个名为“打印”的按钮,以便用户可以打印那张卡片。我谷歌了一下,但没有找到任何具体的东西。请帮忙解决。

namespace Employee_Card_Manager
{
public partial class Card : Form
{
    public Card(String a, String b, String c, String d, String e, String f, String g, String h, String i)
    {
        InitializeComponent();
        this.label2.Text = a;
        this.label9.Text = b;
        this.label10.Text = c;
        this.label11.Text = d;
        this.label12.Text = e;
        this.label13.Text = f;
        this.label14.Text = g;
        this.label16.Text = h;
        this.pictureBox1.Image = Image.FromFile(i);
        Image myimg = Code128Rendering.MakeBarcodeImage(h, 2, true);
        this.pictureBox2.Image = myimg;
    }
    private void button1_Click(object sender, EventArgs e)
    {
          //Print Card Code
    }
  }
}

卡片模板如下所示:

员工卡片结构

我已经将所有的卡片结构放在一个面板控件上,并将面板背景设置为白色。请问你能否提供打印此卡片的代码?谢谢。

请查看此链接:http://msdn.microsoft.com/zh-cn/library/6he9hz8c.aspx - Rahul
2个回答

20
我找到了下面这段代码,它完美地运行了!!
    //declare event handler for printing in constructor
        printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);

    //Rest of the code
    Bitmap MemoryImage;
    public void GetPrintArea(Panel pnl)
    {
        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (MemoryImage != null)
        {
            e.Graphics.DrawImage(MemoryImage, 0, 0);
            base.OnPaint(e);
        }
    }
    void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
    {
        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y);
    }
    public void Print(Panel pnl)
    {
        pannel = pnl;
        GetPrintArea(pnl);
        previewdlg.Document = printdoc1;
        previewdlg.ShowDialog();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Print(this.panel1);
    }

2
你好Nida,能否请你定义一下Print方法中的Pannel是什么?谢谢。 - XTGX
为什么第一行会被红色下划线标记? printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage); - r.hamd
3
好的,我会尽力完成您的翻译要求。以下是需要翻译的内容:「here we go, your copy paste has missed few lines in the begining: here is the link i found: http://rkinfopedia.blogspot.com/2008/07/printing-contents-of-panel-control.html」「这里我们来了,你复制粘贴时少了几行开头:我找到了一个链接: http://rkinfopedia.blogspot.com/2008/07/printing-contents-of-panel-control.html」 - r.hamd

0

这个MSDN页面对你不起作用吗? http://msdn.microsoft.com/en-us/library/aa287529(v=vs.71).aspx

编辑:我修改了上述链接中的示例,并创建了一个PrintablePanel类,您可以在各种表单中重复使用:

public partial class PrintablePanel : Panel
{
    public PrintablePanel()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {

        base.OnPaint(e);
    }

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


    public Bitmap GetImage()
    {
        Bitmap memoryImage = null;
        Graphics mygraphics = CreateGraphics();

        Size s = this.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, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);

        return memoryImage;
    }

据我理解,这段代码是用来打印整个表单的,而在我的情况下,我的意图是只打印面板... - Azeem
因此,如果您使用链接中的示例代码,则应使用panel1而不是使用此内容。 - Mike Corcoran
@svenv,感谢您的努力。您能告诉我如何使用那个类吗?因为在我的员工卡类中没有名为Print的函数,我应该调用它... - Azeem
将一个名为“PrintablePanel”的新用户控件添加到您的项目中,并粘贴上面的代码。然后构建您的项目,进入“Card”表单的表单编辑器并添加PrintablePanel。将现有面板中的所有控件移动到PrintablePanel1中。然后按照MSDN文章中的说明,添加PrintDocument,实现printDocument1_PrintPage事件处理程序,并在调用PrintPanel1.GetImage()并存储结果后调用PrintDocument1.Print()。 - svenv
@svenv 谢谢,我会尽快回复你! - Azeem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接