Quick to create barcode with specified width & height using C#
QR Code dimension width & height
- Create a new QRCode object with data message set
- With property BarcodeWidth to 1500 pixels. The generated QR Code square will be 1500 pixels in width and height.
QRCode barcode = new QRCode(); barcode.Data = "QR Code"; barcode.BarcodeWidth = 1500; // 1500 pixels barcode.drawBarcode("C://Output//csharp-barcode-size-qrcode.png");
Data Matrix 2d barcode dimension width & height
- Create a new DataMatrix object with 2d data text encoding
- With property BarcodeWidth to 900 pixels. The generated Data Matrix square will be 900 pixels in width and height.
DataMatrix barcode = new DataMatrix(); barcode.Data = "Data Matrix"; barcode.BarcodeWidth = 900; // 900 pixels / points barcode.drawBarcode("C://Output//csharp-barcode-size-data-matrix.png");
Ractangular Data Matrix
Most of Data Matrix symbals are in square. However the Data Matrix specification has defined 6 ractangular Data Matrix formats.
To create a rectangular Data Matrix with your specified width and height, you need provide the same image width vs height ratio with Data Matrix columns vs rows ratio.
The C# code above will generate a rectangle Data Matrix barcode with 12 modules in height, and 36 modules in width.
Please note that the Data Matrix with 12x36 format can encode no more than 31 characters. If your data length is greater than 31, the barcode generator library will automatically choose a right data format for you.
DataMatrix barcode = new DataMatrix(); barcode.Data = "Data Matrix"; barcode.FormatMode = DataMatrixFormatMode.Format_12X36; barcode.BarcodeWidth = 600; barcode.BarcodeHeight = 200; barcode.drawBarcode("C://Output//csharp-datamatrix-size-rectangular.png");
The C# code above will generate a rectangle Data Matrix barcode with 12 modules in height, and 36 modules in width.
Please note that the Data Matrix with 12x36 format can encode no more than 31 characters. If your data length is greater than 31, the barcode generator library will automatically choose a right data format for you.
PDF-417
PDF-417 symbol is a 2D barcode in rectangle. Creating a PDF-417 barcode image, you need specify both BarcodeWidth and BarcodeHeight values.
PDF417 barcode = new PDF417(); barcode.Data = "PDF-417"; barcode.BarcodeWidth = 600; barcode.BarcodeHeight = 200; barcode.drawBarcode("C://Output//pdf417-size-sample.png");
1D / Linear barcode dimension width & height
- Create a new Linear object with barcode type as Code 128, and barcode data text encoded.
- Same as the above sample, set AutoResize to true, UOM (unit of measure) to Inch, Resolution to 900 dpi (dots per inch)
- With property BarcodeWidth to 1 inch and BarcodeHeight to 0.6 inch. The generated Code 128 rectangle will be 1 inch in width, and 0.6 inch in height.
Linear barcode = new Linear(); barcode.Type = BarcodeType.CODE128; barcode.Data = "Code 128 - 1234567890"; barcode.AutoResize = true; barcode.UOM = UnitOfMeasure.INCH; barcode.Resolution = 900; // 900 dot (pixel) per inch barcode.BarcodeWidth = 1; // 1 inch barcode.BarcodeHeight = 0.6f; barcode.drawBarcode("C://Output//csharp-barcode-size-code128.png");
EAN/UPC add-on barcode width & height
EAN/UPC (EAN-13, EAN-8, UPC-A, UPC-E) and ISBN, ISSN may have 2 or 5 digits add-on barcode. When you use C# barcode generator library to create these barcodes
with add-on, you can apply the following properties to control barcode width and height.
- AutoResize. When it is true, the barcode library will draw the whole barcode image by input "BarcodeWidth" and "BarcodeHeight" property values.
- UOM. Specify the size related settings unit of measure.
- Resolution Image resolution dot per inch.
- BarcodeWidth Define generated barcode image width
- BarcodeHeight Define generated barcode image height
- TextMargin Specify the space between linear barcode and barcode human readable text
- SupHeight Specify the ratio for add-on bar height vs main bar height
- SupSpace The space between main bar and add-on bar.
Linear barcode = new Linear(); barcode.Type = BarcodeType.EAN13_2; barcode.Data = "012345678912"; // 12 or 13 digits barcode.SupData = "01"; barcode.SupHeight = 0.6f; barcode.SupSpace = 0.06f; barcode.TextFont = new Font("OCR-B", 12f, FontStyle.Regular); barcode.TextMargin = 0.01f; barcode.AutoResize = true; barcode.UOM = UnitOfMeasure.INCH; barcode.Resolution = 400; // 400 dot (pixel) per inch barcode.BarcodeWidth = 1; // 1 inch barcode.BarcodeHeight = 0.4f; barcode.drawBarcode("C://Output//barcode-sample-ean13-2-digit.png");
Advanced Barcode Size Customization Settings
Customze barcode padding area
When you change the size of the barcode in AutoResize mode (AutoResize is true), sometimes you may find some extra space around the barcode.
You can hide the extra space by setting the color of the space to be transparent. Here are the sample code.
You can hide the extra space by setting the color of the space to be transparent. Here are the sample code.
QRCode barcode = new QRCode(); barcode.Data = "QR Code"; barcode.AutoResize = true; barcode.BarcodeWidth = 300; barcode.AutoResizePaddingTransparent = true; barcode.drawBarcode("C://Output//csharp-barcode-size-qrcode.png");
The second image below is created when AutoResizePaddingTransparent is false.
|
|
1D barcode text area settings
When you change the size of the 1d barcode in AutoResize mode (AutoResize is true), the barcode text area will automatically be resized.
If you need manually adjust the barcode text area settings, such as margin space between text and barcode, text font family or size. You can try the following C# code.
If you need manually adjust the barcode text area settings, such as margin space between text and barcode, text font family or size. You can try the following C# code.
- Create a barcode with specified width and height using AutoResize mode.
- Set property AutoResizeBarcodeText to false. Now you can manually apply the barcode text size related options.
- Set TextMargin to the space between barcode and text
- Set TextFont with your target text font family and size
Linear barcode = new Linear(); barcode.Type = BarcodeType.CODE128; barcode.Data = "Code 128 - 1234567890"; barcode.AutoResize = true; barcode.UOM = UnitOfMeasure.INCH; barcode.Resolution = 900; // 900 dot (pixel) per inch barcode.BarcodeWidth = 1; // 1 inch barcode.BarcodeHeight = 0.6f; barcode.AutoResizeBarcodeText = false; barcode.TextMargin = 0.0333f; barcode.TextFont = new Font("OCR-B", 20); barcode.drawBarcode("C://Output//csharp-barcode-size-code128-text.png");
How to estimate or calculate created barcode width & height using C#?
If you need to calcuate the barcode image width and height before the barcode generation, you can try the following API defined in class Barcode in C# barcode library.
The above API is for our internal usage. You can set parameter allowFloat to true, when your generated barcode is in vector (SVG or EPS) image formats.
public SizeF estimateBarcodeSizeInPixels(bool allowFloat)
The above API is for our internal usage. You can set parameter allowFloat to true, when your generated barcode is in vector (SVG or EPS) image formats.
Get the generated barcode width and height
If you have applied the barcode options, and called method drawBarcode() to generate barcode. You can use the following methods
to get the precise generated barcode image width and height in pixel.
public int getGeneratedImageWidthInPixel() public int getGeneratedImageHeightInPixel()
View and print barcode image with specified size in ASP.NET web app
Here we will demonstrate how to generate and display a barcode with specific dimensions on a web page, and ensure that when using the browser's print function, the printed barcode retains those exact dimensions.
In the demo code below, we create a Code 128 barcode image on an ASPX web page. The barcode image width is 4 inches, and the height is 1.6 inches. When we use the browser's print function, the printed barcode will still maintain these dimensions, 4 inches by 1.6 inches.
In the demo code below, we create a Code 128 barcode image on an ASPX web page. The barcode image width is 4 inches, and the height is 1.6 inches. When we use the browser's print function, the printed barcode will still maintain these dimensions, 4 inches by 1.6 inches.
Step 1. Create Barcode with specified size in the C# code
You can create, print barcode image with specified size using the following C# code in asp.net web application.
- Set property
UOMwith the specified unit of measure. You can choose inch, cm and pixel. Here we will useUnitOfMeasure.INCH - Set property
BarcodeWidthwith the target image width in inch. - Set property
BarcodeHeightwith the target image height in inch.
Linear barcode = new Linear(); barcode.Type = BarcodeType.CODE128; barcode.Data = this.Message; barcode.UOM = UnitOfMeasure.INCH; barcode.BarcodeWidth = 4; barcode.BarcodeHeight = 1.6f; barcode.Resolution = 1200;
Step 2. Render barcode image with specified size in the web page
Browsers do not natively support reading the resolution information embedded in image formats such as PNG or JPEG. Therefore, when displayed on a web page, images are rendered using a default DPI setting. Typically it is 96 dpi (or other defaults, such as 192). If the image's actual resolution differs from this default, the displayed physical size of the image will deviate from its intended dimensions.
Here in the web page file (
Here in the web page file (
.cshtml or .html) file, we will manually set the image img with the specified size.
<img src="data:image/png;base64,@Model.BarcodeImage" style="width: 4in; height: 1.6in; border: 3px solid #0066ff;" />
Step 3. Verify the printed barcode width and height.
- Run the ASP.NET web application, and create, display the barcode in web browser.
- Use browser build-in printer to print the barcode web page to a PDF file
Note:- Use a relatively large Page size to prevent the image from being automatically scaled during conversion due to page size limitations.
- Set the Scale option to a fixed value of 100% to avoid automatic scaling of the image. Note that this percentage actually scales the entire page. For example, setting it to 200% will double the image size when converted to a PDF page.
-
Open PDF file using Acrobat pro. And measure the printed barcode image.
Complete barcode size related properties in C# Barcode Generator library
We have listed the complete barcode size customization properties. You can view details here:
List of barcode size customization properties in barcode library
List of barcode size customization properties in barcode library