FAQ TBarCode DLL / TBarCode Library
How Can I Create a Bitmap With a Specific Module Width?
How Can I Create a Bitmap With a Specific Module Width?
In the code below you just set the module width in[mm] and the total bar code height in[mm]. Via the optimizeToDPI flag you can add also an optimization to the module width (in fact you get the same result as with BCGetOptimalBitmapSize). The sample code is for linear bar codes, but can be adapted to 2D codes as well.
Call this after BCCreate (..)
double moduleWidthUserMM = 0.26; double heightUserMM = 22.0; double dpi = 600; int widthUserPx = 1; // dummy value, since the width of the bar code is determined by the module width int heightUserPx = dpi * heightUserMM / 25.4; // for bitmaps we need [Pixel] double width, height; bool optimizeToDPI = TRUE; // adapt module width to next lower full Pixels => avoid quantization errors int fontSizePt = (int)(0.51 + (10 * moduleWidthUserMM / 0.339)); // auto font size - works well with EAN / Code-128 / Code 39 // bounding rectangle of barcode -> desired drawing coordinates RECT rect; rect.left = 0; rect.right = widthUserPx; rect.top = 0; rect.bottom = heightUserPx; eCode = BCSetDPI(pBarCode, dpi, dpi); eCode = BCSetIgnoreDpiFromDC(pBarCode, TRUE); // Ensure that the width of a bar is a multiple of a single pixel (and at minimum one pixel) eCode = BCSetOptResolution(pBarCode, optimizeToDPI); // bar code should adapt to module width eCode = BCSetSizeMode(pBarCode, e_SizeMode::eSizeMode_CustomModuleWidth); // set module width in [0.001mm] // we need a rounding delta of 0.001mm in case of optimize flag eCode = BCSetModWidthN(pBarCode, moduleWidthUserMM * 1000 + (optimizeToDPI?1:0)); eCode = BCSetFontHeight(pBarCode, fontSizePt); // Calculate bar code size // for retrieving the size in [Pixel] we need a screen hDC HDC hDC = GetDC(NULL); eCode = BCGetBarcodeSize(pBarCode, &rect, hDC, eMUPixel, &width, &height); if (hDC) DeleteDC(hDC); // Save bitmap with the given module width and size eCode = BCSaveImage(pBarCode, "C:\\Temp\\Barcode-600dpi.jpg", eIMJpg, (int) (width + 0.5), (int) (height + 0.5), dpi, dpi);
Because of the optimize flag enabled, with a given module width of 0.26 mm the resulting module width will be 0.254 mm (6 Pixels @600 dpi).