This project has moved. For the latest updates, please go here.

How do I set the BarcodeWriter resolution to 300 dpi?

Apr 17, 2015 at 3:08 PM
Edited Apr 17, 2015 at 5:11 PM
boxWidthX and boxHeightY is measured in points (PDF's point). I simply convert it to actual measurement as pixels in 300 dpi for the PDF. PDF use dpi of 300, not 96.

So, how do I set the ZXing.NET BarcodeWriter to 300 dpi instead of 96 dpi?
                       var barcodeWriter = new BarcodeWriter
                            //Encoder... ,
                            Format = BarcodeFormat.CODE_39,
                            Options = { Width = (int)((boxWidthX / 72) * 300), Height = (int)((boxHeightY / 72) * 300), PureBarcode = true },  /* PureBarcode - hide the visible 'vin' underneath barcode... */
                            //Renderer = new BitmapRender { TextFont = new Font("Arial", 9, FontStyle.Regular); }  //Helvetica...
                            Renderer = new ZXing.Rendering.BitmapRenderer { Background = dotnetDrawing::Color.Blue }

                        using (var barcodeVin = barcodeWriter.Write(parmBookValues.Vin.Trim()))
                            using (var newMemoryStream = new dotnetIO::MemoryStream())
                                var foo = barcodeVin.Width;

                                barcodeVin.Save(newMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                newMemoryStream.Position = 0;

                                //Get Image...
                                var image = pdfclownEntities::Image.Get(newMemoryStream);

                                    new dotnetDrawing::PointF(
                                    new dotnetDrawing::SizeF(
Apr 21, 2015 at 5:38 AM
You can't set the dpi (dot per inch) of the resulting image with the width and the height values.
Width and height only defines the "dots per image" not the "dots per inch".
At the moment you have the following options:
  1. wrote your own BitmapRenderer class which sets the needed resolution explicitly.
    Take a look at the source code of the original BitmapRenderer class. There is the following line, which needs to be changed
    bmp.SetResolution(96, 96);
  2. create the bitmap with the original BitmapRenderer and copy the contents of the image to a new
    instance of Bitmap with a higher resolution. (I didn't try it out)
  3. create the bitmap with the original BitmapRenderer and call barcodeVin.SetResolution(300, 300); before saving it to
    the memory stream. (I didn't try it out)
The last way would be the easiest one and should work in my opinion. But I didn't test it.
Apr 21, 2015 at 2:21 PM
Is there good examples you may have come across, having all of smaller pieces coming from BitmapRender class? (That can be used).

We're new to ZXing.NET but it will take us some times to figure it out. The only documentation we found is the very technicial info on all objects and very limit sample script (or examples) from the download. So, we're wondering if you or somebody had ever come across some better examples than what we found. That way, we don't have to spend few months fiddling around & figuring it out (one after another that dont works).

Just wondering. Thanks.
Apr 21, 2015 at 6:08 PM
In my opinion the samples are good and showing everything necessary.
And your sample code is correct, too. Only you requirement for a different dpi isn't covered.
But I showed you three possible ways to implement it yourself.
Do you have other requirements for which you didn't find a solution with ZXing.Net?