Calculate the size of the image required to hold the Graphics object

I am writing a barcode application for work where I create a graphic and draw various columns for a barcode of code 39. I create a graphic with the following code:

int reqBarcodeWidth = 100;
int reqBarcodeHeight = 40;

objBitmap = new Bitmap(reqBarcodeWidth, reqBarcodeHeight);

objGraphics = Graphics.FromImage(objBitmap);

      

One of the things I want to do is to allow different units to be used to draw on the graphic, for example:

objGraphics.PageUnit = GraphicsUnit.Inch;

      

If I do this, how do I know what size the bitmap needs to be created to fit the full barcode?

Thanks in advance for your help.

Update:

Thanks for the answer. I've looked at barcode fonts before and really want to do away with installing fonts on a lot of machines.

Also one of the functions of the program, which I did not mention, would be to print stock codes for a warehouse that have no associated EAN codes. These are usually items from China with complex stock codes. They will have to use the Code 128 barcode, which uses an extended character range and also takes up less space.

The hard bit is mostly done just to create an image that I need to sort out. I don't know if I am trying to do this in a complex way and have not done any work on images before.

+1


a source to share


2 answers


Did you know that there are fonts that draw a barcode so you can avoid all this mess?



+2


a source


If I do this, how do I know what size the bitmap needs to be created to fit the full barcode?

Bitmaps are based on physical devices that have specific resolutions, usually measured in DPI (dots per inch). Windows OS sets the logical resolution to 96 or 120 dpi for the monitor. Printers have a physical resolution from 300 to 600, and sometimes up to 1200 or more. Images that render a movie can have physical resolutions up to 2540 DPI or more.

Boolean resolution implies that the X points are equal to one inch on a particular device. This is usually an approximation and can be used for scaling. Physical resolution indicates that the device can (or is physically present) X dots per inch.

When creating barcodes and printing on a device, you must consider the resolution of that device. If you have a 4 "barcode and want to print it on a 600 dpi printer, you will need a bitmap 4 * 600 = 2400 pixels wide. There are other implications for printing on certain devices, so you don't accidentally decrease or increase code strips.

My point of blurring is that you have to generate your barcodes using a vector format like .WMF or .EMF and then map it to a bitmap that is configured for the device you want to output. It will also allow you to edit codes in a vector drawing program such as CorelDRAW or Adobe Illustrator.

In most cases, if not all barcodes have a width formula that will indicate the width in inches of the final code with a specific encoding (symbology encoded in the barcode).

With this information, you can make barcodes using something similar to the following (copy and paste this vb.net code into the form.load event or click button for a new project):


Dim gfx As Graphics = Me.CreateGraphics
Dim hdc As IntPtr = gfx.GetHdc()
Dim mf As System.Drawing.Imaging.Metafile = New Imaging.Metafile("c:\1.wmf", hdc, New RectangleF(0, 0, 5 * 2540, 1 * 2540), Imaging.MetafileFrameUnit.GdiCompatible)
Dim g As Graphics = Graphics.FromImage(mf)
g.PageUnit = GraphicsUnit.Inch

g.FillRectangle(Brushes.Blue, New RectangleF(0, 0, 5, 1))
g.FillRectangle(Brushes.Red, New RectangleF(0.25, 0.25, 4.5, 0.5))

g.Dispose()
mf.Dispose()
gfx.ReleaseHdc(hdc)
gfx.Dispose()

      



  • The metafile constructor size is inches * 2540.2540 is a constant representing 0.01 millimeters per inch. This is what the underlying GDI uses.

Now open the file "1.wmf" with a real vector graphics program such as CorelDRAW or Illustrator. (You cannot use MSPaint or Windows Preview, etc.). You will notice that you have a blue 5x1 rectangle with a 4.5 x 0.5 red rectangle inside.

This is how your barcodes should be sucked into the metafile. Replace the blue and red rectangle with code using the barcode drawing procedure. Remember to create a transparent dimension frame to tell the metafile the border of your drawing.


g.FillRectangle(Brushes.Transparent, rectBounds)

      

Once you've drawn the barcode and saved it to the metafile, you can load it using GDI + and map it to a bitmap that has the resolution of your output device. Obviously, the last step is to send the bitmap to your output device, unless of course your output device has a vector or postscript processor built in.

Well, that was probably more than you ever wanted to know about the topic, but this is the right way to do it, and it will save you a lot of time and heartache if your codes are ever accused of what they are. " unsafe "" ...

Good luck and let me know how it turns out.

+2


a source







All Articles