How to print images, pictures, texts and high quality barcodes using VB.NET or C#
Product Barcode Professional SDK for .NET Published 12/07/2010 Updated 12/07/2010
Overview
In this guide you will learn how to print images or pictures, texts and barcodes by using Barcode Professional SDK and .NET PrintDocument class.
Requirements
Neodynamic Barcode Professional SDK 3.0 for .NET
Microsoft .NET Framework 2.0 or greater
Microsoft Visual Studio 2005 / 2008 or Microsoft Visual Basic Express or Visual C# Express Editions
NOTE
Please download the following image to C:\temp folder before running the sample code.
Please download the following image to C:\temp folder before running the sample code.
Follow up these steps
- Open Visual Studio and create a new Windows Forms app using your preferred .NET language VB or C#
- In your project add a reference to Neodynamic.SDK.Barcode.dll
- Open the default form in design view and drag & drop a Button control, a PrintDialog and PrintDocument components from VS toolbox
- Double-click on the Button control and paste the following code inside the button's Click event handler
VB
'show print dialog... If printDialog1.ShowDialog() = DialogResult.OK Then 'set printer settings on printdocument object printDocument1.PrinterSettings = printDialog1.PrinterSettings 'print... printDocument1.Print() End IfC#
//show print dialog... if (printDialog1.ShowDialog() == DialogResult.OK) { //set printer settings on printdocument object printDocument1.PrinterSettings = printDialog1.PrinterSettings; //print... printDocument1.Print(); } - Back to the form design view and now double-click on the PrintDocument component. In the code behind class, paste the following code inside the PrintDocument's PrintPage event handler
VB
'printing barcode coupon... '1. Print the company logo and % for coupon... Dim logo As Image = Image.FromFile(@"C:\temp\AWCoupon.jpg") e.Graphics.DrawImage(logo, New Point(0, 0)) logo.Dispose() '2. Print barcode... Dim barcode As New Neodynamic.SDK.Barcode.BarcodeProfessional() 'set barcode symbology... eg Code 128 barcode.Symbology = Neodynamic.SDK.Barcode.Symbology.Code128 'set barcode bars dimensions... barcode.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch barcode.BarWidth = 0.015 barcode.BarHeight = 0.75 'set value to encode... this time a random value... barcode.Code = Guid.NewGuid().ToString().ToUpper().Substring(0, 16) 'justify text... barcode.CodeAlignment = Neodynamic.SDK.Barcode.Alignment.BelowJustify 'set font for barcode human readable text... Dim fnt As New Font("Courier New", 10F) barcode.Font = fnt fnt.Dispose() 'print barcode below logo... 'IMPORTANT: barcode location unit depends on BarcodeUnit setting which in this case is INCH barcode.DrawOnCanvas(e.Graphics, New PointF(0.1F, 1.5F)) '3. Print some texts... Dim fnt1 As New Font("Arial", 12F, FontStyle.Bold) Dim fnt2 As New Font("Arial", 8F, FontStyle.Regular) e.Graphics.DrawString("RESELLER INTRUCTIONS", fnt1, Brushes.Black, New PointF(10F, 270F)) e.Graphics.DrawString("Use Item -% Off for the 15%. Scan coupon barcode or enter coupon code. Collect coupon with purchase as coupon may only be redeemed once per customer.", fnt2, Brushes.Black, New RectangleF(New PointF(10F, 290F), New SizeF(570F, 50F))) e.Graphics.DrawString("COUPON OFFER DETAILS", fnt1, Brushes.Black, New PointF(10F, 350F)) e.Graphics.DrawString("This coupon can be redeemed once at any Adventure Works Cycles retail store." + Environment.NewLine + "This coupon is valid from May 21, 2009 to Jun 21, 2009.", fnt2, Brushes.Black, New RectangleF(New PointF(10F, 370F), New SizeF(570F, 50F))) fnt1.Dispose() fnt2.Dispose()C#
//printing barcode coupon... //1. Print the company logo and % for coupon... using (Image logo = Image.FromFile(@"C:\temp\AWCoupon.jpg")) { e.Graphics.DrawImage(logo, new Point(0, 0)); } //2. Print barcode... using (Neodynamic.SDK.Barcode.BarcodeProfessional barcode = new Neodynamic.SDK.Barcode.BarcodeProfessional()) { //set barcode symbology... eg Code 128 barcode.Symbology = Neodynamic.SDK.Barcode.Symbology.Code128; //set barcode bars dimensions... barcode.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch; barcode.BarWidth = 0.015; barcode.BarHeight = 0.75; //set value to encode... this time a random value... barcode.Code = Guid.NewGuid().ToString().ToUpper().Substring(0, 16); //justify text... barcode.CodeAlignment = Neodynamic.SDK.Barcode.Alignment.BelowJustify; //set font for barcode human readable text... using (Font fnt = new Font("Courier New", 10f)) { barcode.Font = fnt; } //print barcode below logo... //IMPORTANT: barcode location unit depends on BarcodeUnit setting which in this case is INCH barcode.DrawOnCanvas(e.Graphics, new PointF(0.1f, 1.5f)); } //3. Print some texts... using (Font fnt1 = new Font("Arial", 12f, FontStyle.Bold)) { using (Font fnt2 = new Font("Arial", 8f, FontStyle.Regular)) { e.Graphics.DrawString("RESELLER INTRUCTIONS", fnt1, Brushes.Black, new PointF(10f, 270f)); e.Graphics.DrawString("Use Item -% Off for the 15%. Scan coupon barcode or enter coupon code. Collect coupon with purchase as coupon may only be redeemed once per customer.", fnt2, Brushes.Black, new RectangleF(new PointF(10f, 290f), new SizeF(570f, 50f))); e.Graphics.DrawString("COUPON OFFER DETAILS", fnt1, Brushes.Black, new PointF(10f, 350f)); e.Graphics.DrawString("This coupon can be redeemed once at any Adventure Works Cycles retail store." + Environment.NewLine + "This coupon is valid from May 21, 2009 to Jun 21, 2009.", fnt2, Brushes.Black, new RectangleF(new PointF(10f, 370f), new SizeF(570f, 50f))); } } - That's it! Run the app, click on the button and after selecting a printer the following output should be printed out.
A barcode coupon sample generated and printed by using Barcode Professional SDK and PrintDocument
Useful? Share it!
| Tweet |
|
Related Articles
How to create local reports RDLC featuring barcode images in .NET SDKHow to create Crystal Reports featuring barcode images using Typed DataSet in .NET SDK
Need help? Contact Support
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.