FAQ TBarCode DLL / TBarCode Library
How to Draw the Barcode with Different DPI in X and Y Axis?
How to Draw the Barcode with Different DPI in X and Y Axis?
The barcode generator library does not support bitmap export with anisotropic resolution (DPI Y != DPI X). Whenever we have to create our own DC, we create it “isotropic” (DPI Y = DPI X). But there are workarounds as shown below.
Workarounds:
Example - you need DPI X = 1200, DPI Y = 600
A) Anisotropic Device Context
- Use BCDraw (hDC) with a device context, where DPI X = 1200 and DPI Y = 600 is already preset to these values.
- Use Draw(gfx) with a graphics object, where the internal DC is preset to DPI X = 1200 and DPI Y = 600.
- Either don’t call BCSetDPI or use BCSetDPI (pBarcode, -1, -1) ➔ resets the Library DPI to DC default values.
- Don’t set BCIgnoreDPIFromDC because we need the values from the DC or graphics object.
- If you need the bar code image in a bitmap file, you have to create it with your own code from the drawn image in memory.
B) Scale the Image
C/C++ Library
First draw to a bitmap DC with 1200 x 1200 dpi (isotropic) and later scale the image in the Y-axis to 50% = 600 dpi to get your anisotropic result.
- You could use the code from FAQ Drawing to Bitmap-DC and Scaling to Screen-DC as basis.
- When copying the image, use StretchBlt instead of BitBlt to achieve the scaling of the Y-axis (use an ROP and Stretch Mode, which does not create aliasing or softened edges)
- https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-stretchblt
- https://learn.microsoft.com/en-us/windows/win32/gdi/scaling-an-image - Let TBarCode draw with a high resolution even on a low resolution screen DC:
If your Bitmap DC is based upon screen resolution (~ 96 dpi), but you want 1200 dpi or something else, you can use the API function BCSetIgnoreDPIFromDC (pBarcode, TRUE) - set it before BCDraw - in combination with BCSetDPI (pBarCode, 1200, 1200). This draws a high resolution bitmap by internally scaling the barcode drawing coordinates. Note that DPI X and DPI Y must be equal here, the target bitmap DC must be large enough to hold the full picture (take care on width/height). The same works also in .NET with similar API functions.
If you need a BMP/JPG/PNG file from your image in memory, you have to use additional bitmap export code (free libraries and sample code publicly available on the web).
.NET API
If you use our .NET API you can do something similar as with StretchBlt (GDI) with the DrawImage() function. First create a “normal” Barcode bitmap and then stretch it or change the DPI – see sample code (or google for more):
C) Custom Drawing
Use our Callback drawing mechanism. You get all the coordinates – either in Millimeters or in a matrix pattern (row pattern). Then you decide on your own, how you scale the X and Y axis coordinates you receive from the library. You receive coordinates in 1/1000 mm resolution from the library and can convert it to Pixel with your own code (and apply your own DPI to X and Y axis). Then draw to a Bitmap DC (GDI Filled Rectangle) or use a Graphics library, which does the work for you (e.g. NET).
Sample code for Custom Drawing (installed with the SDK):
C:\ProgramData\TEC-IT\TBarCode\11.15\Examples\Visual C++\Custom DrawingC:\ProgramData\TEC-IT\TBarCode\11.15\Examples\Visual Studio .NET\CustomDrawing
More details:
- TBarCode11 DLL Developer Manual - section “Custom Drawing”
- TBarCode Library Customized Drawing (Callbacks)
Callback drawing is available in the native C/C++ DLL and also in the .NET API (see sample above).