FAQ TBarCode .NET
How Can I Control the Bar Code Size?
How Can I Control the Bar Code Size?
You can chose between two size modes:
- Size Mode Fit to Bounding Rectangle
The barcode adapts to the bounding rectangle by auto-calculation of the module width. The module width property is just a Get/Set value and is not updated. - Size Mode Custom Module Width
The barcode size depends on the module width property which is set in [mm]. The barcode still adapts to the bounding rectangle in the dimension where the module width is not relevant (like the height in a linear code).
The Bounding Rectangle must be supplied in [mm] for doing Size calculations like with CalculateModuleWidth or CalculateBarcodeSize.
The Bounding Rectangle must be supplied in [Pixel] for any bitmap or device context related drawing operations.
Recommended Workflow
For any operations first set the rectangle, then do the operation or calculation.
It is recommended to do all in [mm] first and in case you draw to a bitmap, convert the mm to Pixel considering also the DPI you have adjusted.
- Set bar code and bar code data
- Set bounding rectangle in [mm], set the size mode and if applicable the module width
- Set the DPI if applicable
- If you use a custom module width: Do size retrieval / calculation functions (returned values are in mm). This is required because your bounding rectangle may not reflect the real bar code size.
- If you need a bitmap, convert the returned size to Pixel and set the bounding rectangle in Pixel
Use the BoundingRectangleF which is using "float" instead of integer.
For creating bitmaps where the barcode size is not predefined by any specific requirements there is also the function CalculateOptimalBitmapSize. This function avoids quantization errors and improves the quality.
barcode.Dpi = 100; barcode.SizeMode = SizeMode.FitToBoundingRectangle; Size optimalSize = barcode.CalculateOptimalBitmapSize(null, 1, 1); // 1 Module = 1 Pixel barcode.BoundingRectangle = new Rectangle(0, 0, optimalSize.Width, optimalSize.Height); barcode.Draw(filename, ImageType.Jpg);