Articles in this section
Category / Section

How to apply transparent background to barcode using C# and VB.NET in WinForms PDF?

3 mins read

Steps to apply transparent background to the barcode programmatically:

  1. Create a new C# console application project. The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can apply transparent background to the barcode using C# and VB.NET.

create new C# application project

2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework     application from Nuget.org.

NuGet package reference

  1. Including the following namespace in Program.cs file.

C#

using Syncfusion.Pdf.Barcode;
using System.Drawing;
using System.IO;

 

VB.NET

Imports Syncfusion.Pdf.Barcode
Imports System.Drawing
Imports System.IO

 

  1. Use the following code to apply transparent background to the barcode using C# and VB.NET.

C#

//Create a bitmap.  
Bitmap bmp = new Bitmap(200, 200);
 
//Create graphics from the bitmap.   
Graphics graphics = Graphics.FromImage(bmp);
 
//Drawing QR Barcode.  
PdfQRBarcode barcode = new PdfQRBarcode();
 
//Set the Error correction level.  
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
 
//Creates a new PdfBarcodeQuietZones.
PdfBarcodeQuietZones quietZones = new PdfBarcodeQuietZones();
quietZones.All = 0f;
 
//Set the XDimension.  
barcode.XDimension = 3;
 
//Add the barcode text.
barcode.Text = "http://www.syncfusion.com";
 
//Apply the quiet zone. 
barcode.QuietZone = quietZones;
 
//Convert the barcode to an image.  
System.Drawing.Image barcodeImage = barcode.ToImage();
 
//Set transparency to the image.     
Bitmap bitmap = (Bitmap)barcodeImage;
bitmap.MakeTransparent(System.Drawing.Color.Transparent);
 
//Save the transparency applied image. 
MemoryStream imageStream = new MemoryStream();
bitmap.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
 
//Load the transparency applied image.  
System.Drawing.Image transparencyImage = System.Drawing.Image.FromStream(imageStream);
 
//Fill the rectangle with transparency.   
Brush brush = new SolidBrush(System.Drawing.Color.FromArgb(128, 128, 128, 128));
graphics.FillRectangle(brush, 0, 0, 200, 200);
 
//Draw the transparency image with the bitmap graphics. 
graphics.DrawImage(transparencyImage, new System.Drawing.RectangleF(0, 0, 200, 200));
 
//Save the transparency image.  
bmp.Save("Barcode.png", System.Drawing.Imaging.ImageFormat.Png);
 
//Dispose all the objects. 
graphics.Dispose();
bmp.Dispose();
bitmap.Dispose();

 

VB.NET

'Create a bitmap.  
Dim bmp As Bitmap = New Bitmap(200, 200)
 
'Create graphics from the bitmap.    
Dim graphics As Graphics = Graphics.FromImage(bmp)
 
'Drawing QR Barcode.  
Dim barcode As PdfQRBarcode = New PdfQRBarcode()
 
'Set the Error correction level.   
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High
 
'Creates a new PdfBarcodeQuietZones. 
Dim quietZones As PdfBarcodeQuietZones = New PdfBarcodeQuietZones()
quietZones.All = 0F
 
'Set the XDimension.   
barcode.XDimension = 3
 
'Add the barcode text.
barcode.Text = "http://www.syncfusion.com"
 
'Apply the quiet zone. 
barcode.QuietZone = quietZones
 
'Convert the barcode to an image  
Dim barcodeImage As System.Drawing.Image = barcode.ToImage()
 
'Set transparency to the image      
Dim bitmap As Bitmap = CType(barcodeImage, Bitmap)
bitmap.MakeTransparent(System.Drawing.Color.Transparent)
 
'Save the transparency applied image. 
Dim imageStream As MemoryStream = New MemoryStream()
bitmap.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png)
 
'Load the transparency applied image.   
Dim transparencyImage As System.Drawing.Image = System.Drawing.Image.FromStream(imageStream)
 
'Fill the rectangle with transparency.    
Dim brush As Brush = New SolidBrush(System.Drawing.Color.FromArgb(128, 128, 128, 128))
graphics.FillRectangle(brush, 0, 0, 200, 200)
 
'Draw the transparency image with the bitmap graphics. 
graphics.DrawImage(transparencyImage, New System.Drawing.RectangleF(0, 0, 200, 200))
 
'Save the transparency bitmap.   
bmp.Save("Barcode.png", System.Drawing.Imaging.ImageFormat.Png)
 
//Dispose all the objects. 
graphics.Dispose()
bmp.Dispose()
bitmap.Dispose()
 

 A complete working sample can be download from BarcodeImageSample.zip.

By executing the program, you will get the image file as follows.

 

Generated barcode image

 

Take a moment to peruse the documentation. You can find the options like adding one-dimensional barcode and two-dimensional barcode to PDF document, adding a barcode to the PDF document without displaying the barcode text, export barcode as an image, customizing barcode appearance.


Refer to this link to explore a rich set of Syncfusion Essential PDF features.


Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without trail message.


Also see:

Add barcode in the PDF document

Set width to barcode in the PDF document

Align barcode text in the PDF document

Convert barcode to image ASP.NET Core platform

 

 

Conclusion

I hope you enjoyed learning about how to apply transparency to the generated barcode image in WinForms PDF.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations documentation and how to quickly get started for configuration specifications.  You can also explore our WinForms PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied