FAQ TBarCode DLL / TBarCode Library
Come posso creare una bitmap con una larghezza modulo specifica?
Come posso creare una bitmap con una larghezza modulo specifica?
Nel codice sottostante impostate semplicemente la larghezza modulo in [mm] e l'altezza totale del codice a barre in [mm]. Tramite il flag optimizeToDPI potete aggiungere anche un'ottimizzazione alla larghezza del modulo (di fatto ottenete lo stesso risultato di BCGetOptimalBitmapSize). Il codice di esempio è per codici a barre lineari, ma può essere adattato anche a codici 2D.
Richiamate questo dopo 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);
Poiché il flag di ottimizzazione è abilitato, con una larghezza modulo data di 0,26 mm la larghezza modulo risultante sarà 0,254 mm (6 pixel a 600 dpi).