FAQ TBarCode OCX
How Can I Create an Optimal Barcode Bitmap (1D Code) for 812.8 DPI in VB6?
How Can I Create an Optimal Barcode Bitmap (1D Code) for 812.8 DPI in VB6?
Approach 1
The first approach uses a pre-set (custom) module width, sets the DPI and then calculates the total width of the symbol. The principle works for other printers, too.
The OCX method BCWidthHdc2 can be used to calculate the width of the barcode. If a module width has been set, use a dummy value for the [in] nWidth. If you calculate the width in unit=mm, convert it to Pixels afterwards for the SaveImage function. There is a similar function for the height – but the height needs calculation only for 2D barcodes.
Note: When you set a module width of exactly 0.3125mms or 0.0123 inches you are 100% in the printing raster of a 812.8 dpi machine. This is one of the recommended values. If you have enabled the opt resolution flag, the module width is changed to the next (lower) pixel boundaries in the printing raster and therefore “optimized”. Dpi must be set accordingly for this optimization.
Sample Code:Dim widthPx As Long Dim heightPx As Long Dim heightMM As Double TBarCode111.Dpi = 812.8 TBarCode111.SizeMode = eSizeMode_CustomModuleWidth TBarCode111.ModuleWidth = "312.5" heightPx = 500 heightMM = heightPx * (25.4 / TBarCode111.Dpi) widthPx = TBarCode111.BCWidthHdc2(0, 1000, heightMM * 1000, eMUPixel) TBarCode111.SaveImage "c:\temp\test2.tif", eIMTif, widthPx, heightPx, 812.8, 812.8
Approach 2
With the second, more simple approach, the module width is not set explicitly. Instead the scaling factor passed on to the GetOptimalBitmapSize (OCX API) function controls the module width. If you use it, don’t set a custom module width and/or optimal resolution in the API. This function performs some additional scaling (based upon dpi), which should be turned off with DPI = 0 before calling the function.
Because we know the DPI of the printer we also know the pixel (printer dot) dimension. We also know that one module should be always an integer multiple of one printer dot. We have to consider that typically one module should not be smaller than 0.190mms at all (but depends on barcode type). Then all you need to do is to pass on the ScaleX = ModuleWidth/PixelSize factor (= 8, 9, 10,…) to the GetOptimalBitmapSize function and use the returned values for SaveImage.
Module width: The 0.0123 inch optimum are 10 Pixels at 812.8 dpi. So the scaling factor would be 10 for the GetOptimalBitmapSize function.
Sample Code:Dim width As Long Dim height As Long width = 1 height = 500 TBarCode111.Dpi = 0 'turn off additional dpi/96 scaling TBarCode111.GetOptimalBitmapSize 10, 10, width, height TBarCode111.SaveImage "c:\temp\test.tif", eIMTif, width, height, 812.8, 812.8