C Bar Code Generation and Printing Instance Code
- 2021-11-10 10:34:22
- OfStack
In this paper, we share the method of C # barcode generation and printing for your reference. The specific contents are as follows
string BarcodeString = "13043404455";// Bar code
int ImgWidth = 520;
int ImgHeight = 120;
// Print button
private void button1_Click(object sender, EventArgs e)
{
// Instantiate a print object
PrintDocument printDocument1 = new PrintDocument();
// Set the paper for printing , You can customize the size of the paper ( Unit: mm). It may not be set when the print height is uncertain
//printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 585, 800);
// Registration PrintPage Event, print every 1 This event is triggered when the
printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
// Start printing
printDocument1.Print();
// Print preview
//PrintPreviewDialog ppd = new PrintPreviewDialog();
//ppd.Document = printDocument1;
//ppd.ShowDialog();
}
// Print Events
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("\r\n\r\n\r\n");
sb.Append("******* Xinglong supermarket *******\r\n");
sb.Append(" Product name ----- Quantity ----- Price \r\n");
sb.Append(" Fine white sand 1 8 Yuan \r\n");
sb.Append(" Betel nut Zhang Xinfa 1 10 Yuan \r\n");
sb.Append(" Total: 2 18 Yuan \r\n");
sb.Append("--- Cashier: Zhang 3---\r\n");
sb.Append("--- Technical support: Li 4---\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
DrawPrint(e, sb.ToString(), BarcodeString, ImgWidth, ImgHeight);
}
/// <summary>
/// Draw print content
/// </summary>
/// <param name="e">PrintPageEventArgs</param>
/// <param name="PrintStr"> Text to be printed </param>
/// <param name="BarcodeStr"> Bar code </param>
public void DrawPrint(PrintPageEventArgs e, string PrintStr, string BarcodeStr, int BarcodeWidth, int BarcodeHeight)
{
try
{
// Draw a print string
e.Graphics.DrawString(PrintStr, new Font(new FontFamily(" Blackbody "), 10), System.Drawing.Brushes.Black, 1, 1);
if (!string.IsNullOrEmpty(BarcodeStr))
{
int PrintWidth = 175;
int PrintHeight = 35;
// Draw a printed picture
e.Graphics.DrawImage(CreateBarcodePicture(BarcodeStr, BarcodeWidth, BarcodeHeight), 0, 0, PrintWidth, PrintHeight);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// Generate barcode pictures according to strings ( To add a reference: BarcodeLib.dll )
/// </summary>
/// <param name="BarcodeString"> Bar code string </param>
/// <param name="ImgWidth"> Picture broadband </param>
/// <param name="ImgHeight"> Picture height </param>
/// <returns></returns>
public System.Drawing.Image CreateBarcodePicture(string BarcodeString, int ImgWidth, int ImgHeight)
{
BarcodeLib.Barcode b = new BarcodeLib.Barcode();// Instantiation 1 Bar code object
BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE128;// Coding type
// Get barcode picture
System.Drawing.Image BarcodePicture = b.Encode(type, BarcodeString, System.Drawing.Color.Black, System.Drawing.Color.White, ImgWidth, ImgHeight);
//BarcodePicture.Save(@"D:\Barcode.jpg");
b.Dispose();
return BarcodePicture;
}
// Preview barcode
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = CreateBarcodePicture(BarcodeString, ImgWidth, ImgHeight);
}