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//OnBarcode.com//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//OnBarcode.com//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//OnBarcode.com//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//OnBarcode.com//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//OnBarcode.com//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()
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
