4

I have a situation here. I need to create an employee card structure in windows form using C# in Visual Studio 2010. The structure may contains labels and picture boxes with white background. I had no problem creating it but I am also giving a button named "Print" on this form so that user can print that card. I googled it but nothing concrete stuff found. Please help me out.

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
    }
  }
}

The card template is as below:

Employee Card Structure

I have placed all card structure on a Panel Control and set Panel background to white. Can you fill the code which will print this card? Thanks

1

2 Answers 2

20

I have found following code which is working perfectly!!

    //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);
    }
3
  • 2
    Hi Nida can u please define what is Pannel in Print Method. thanks
    – XTGX
    Apr 29, 2013 at 10:01
  • why the first line gets underlined with red colore? printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);
    – r.hamd
    Nov 22, 2015 at 20:55
  • 3
    here we go, your copy paste has missed few lines in the begining: here is the link i found: rkinfopedia.blogspot.com/2008/07/…
    – r.hamd
    Nov 22, 2015 at 21:19
0

Does this MSDN page not work for you? http://msdn.microsoft.com/en-us/library/aa287529(v=vs.71).aspx

Edit: I modified the example at aforementioned link and created a PrintablePanel class which you can reuse in various forms:

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;
    }
5
  • As far as I understood this code is to print the whole Form and in my case my intention is to print only the panel...
    – Azeem
    May 15, 2012 at 17:39
  • so instead of using this, you'd be using panel1 if you used the example code at the link. May 15, 2012 at 17:46
  • @svenv thanks for your effort. Can you please tell me how to use that class because there is no function named Print here which I suppose to call in my Employee card class...
    – Azeem
    May 15, 2012 at 18:01
  • Add a new user control "PrintablePanel" to your project and paste in the code from above. Then build your project, go to the form editor for your "Card" form and add a PrintablePanel. Move all controls from your existing panel to the PrintablePanel1. Then as in the MSDN article, add a PrintDocument, implement the printDocument1_PrintPage event handler, and call PrintDocument1.Print() after calling PrintPanel1.GetImage() and storing the result.
    – svenv
    May 15, 2012 at 18:06
  • @svenv thanks, I will get back to you ASAP!
    – Azeem
    May 15, 2012 at 18:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.