- This topic shows how to print image or picture from your system or from your application using C# programming.
- Tools needed: Button1 (Button1), TextBox1 (TextBox1), PrintDocument1 (PrintDocument1).
PrintDocument – How to print image using C# programming
- The PrintDocument control defines the various methods that allow you to send output to the printer.
- To incorporate printing functionality into your Windows application, you can either drag and drop the PrintDocument control onto your form, or create an instance of the PrintDocument class during runtime.
- To start the printing process, use the Print() method of the PrintDocument object. To customize the printing process using the PrintDocument object, there are generally three events that you need to get acquainted with. They are:
- BeginPrint Occurs when the Print() method is called and before the first page of the document prints. Typically, you make use of the BeginPrint event to initialize fonts, file streams, and other resources used during the printing process.
- PrintPage Occurs when the output to print for the current page is needed. This is the main event to code the logic required for sending the outputs to the printer.
- EndPrint Occurs when the last page of the document has printed. Typically, you use the EndPrint event to release fonts, file streams, and other resources used during the printing process, like fonts.
Print image from your system – How to print image using C# programming
- Here you have to assign the location of the image and also where you want the image to display.
Image logo = Image.FromFile("C:\\testimage.jpg");
e.Graphics.DrawImage(logo, 10, 10);
Print image from your application – How to print image using C# programming
- Here you have to assign your application images and its position to display in your print.
e.Graphics.DrawImage(Properties .Resources.ntimage, 10, 10);
Print image that is unscaled and cropped – How to print image using C# programming
- Here you image is cropped, actually it is filled inside the rectangle, you have assign the size the rectangle to display the image.
e.Graphics.DrawImageUnscaledAndClipped(Properties.Resources.ntimage, new Rectangle(10, 40, 60, 40));
How to print image using C# programming – Complete Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string StringToPrint;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
StringToPrint = richTextBox1.Text;
printDocument1.Print();
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringToPrint = richTextBox1.Text;
int numChars = 0;
int numLines = 0;
string stringForPage = null;
StringFormat strFormat = new StringFormat();
Font PrintFont = default(Font);
PrintFont = richTextBox1.Font;
RectangleF rectDraw = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
Image logo = Image.FromFile("C:\\testimage.jpg");
e.Graphics.DrawImage(logo, 10, 10);
// Actual size of the image
e.Graphics.DrawImage(Properties .Resources.ntimage, 10, 10);
// Image be clipped but unscaled
e.Graphics.DrawImageUnscaledAndClipped(Properties.Resources.ntimage, new Rectangle(10, 40, 60, 40));
SizeF sizeMeasure = new SizeF(e.MarginBounds.Width, (e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics)));
strFormat.Trimming = StringTrimming.Word;
e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, out numChars, out numLines);
stringForPage = StringToPrint.Substring(0, numChars);
e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat);
if ((numChars < StringToPrint.Length))
{
StringToPrint = StringToPrint.Substring(numChars);
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
}
}
How to print image using C# programming