Windows Forms: Print Picture Box in C#

This post shows you how to print an image from picture box control in c# .net windows forms application.

To play the demo, you should drag the PictureBox and Button controls from the visual studio toolbox to your winform, then layout your form as shown below.

print picture in c#

Add the click event handler to the Open button allows you to select an image, then display on the PictureBox control.

private void btnOpen_Click(object sender, EventArgs e)
{
    //Open image file
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
            pictureBox.Image = Image.FromFile(ofd.FileName);
    }
}

Add the click event handler to the Print button allows you to print an image from the PictureBox as the following c# code.

private void btnPrint_Click(object sender, EventArgs e)
{
    //Show print dialog
    PrintDialog pd = new PrintDialog();
    PrintDocument doc = new PrintDocument();
    doc.PrintPage += Doc_PrintPage;
    pd.Document = doc;
    if (pd.ShowDialog() == DialogResult.OK)
        doc.Print();
}

Creating the PrintPage event handler allows you to print an image using PrintDocument to the printer.

private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
    //Print image
    Bitmap bm = new Bitmap(pictureBox.Width, pictureBox.Height);
    pictureBox.DrawToBitmap(bm, new Rectangle(0, 0, pictureBox.Width, pictureBox.Height));
    e.Graphics.DrawImage(bm, 0, 0);
    bm.Dispose();
}

Through the c# example, you can easily print an image from the PictureBox in c# windows forms application.

VIDEO TUTORIAL