Ali Elegant
Ali Elegant

Reputation: 81

Barcode i25 using ItextSharp

i am using BarcodeInter25 class to make barcode. I am able to make it but its just blur how can it become more sharp ?? also its background white colour is not completely white My Code:

               BarcodeInter25 code25 = new BarcodeInter25();
               Rectangle r = new iTextSharp.text.Rectangle(38, 152);
               code25.ChecksumText = false;
               code25.Code = "some digits";
               code25.BarHeight = 2
               System.Drawing.Image i = code25.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);

               MemoryStream ms = new MemoryStream();
               i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
               Image img = Image.GetInstance(ms.ToArray());
               ms.Dispose();

Upvotes: 0

Views: 1283

Answers (2)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Looking at your code, it should be obvious why the barcode is blurry. You convert it to a System.Drawing.Image (making it a raster image) and then you convert it to an iTextSharp.text.Image (but by then the image is already blurry).

The correct way to achieve what you want, is to create an iTextSharp.text.Image straight from the barcode (do not pass through System.Drawing.Image). This can be done like this:

BarcodeInter25 code25 = new BarcodeInter25();
Rectangle r = new iTextSharp.text.Rectangle(38, 152);
code25.ChecksumText = false;
code25.Code = "some digits";
code25.BarHeight = 2;
PdfContentByte cb = writer.DirectContent;
Image img = code25.CreateImageWithBarcode(cb, null, null);

Now the Image object won't be a raster image (with pixels that make the lines blurry), but it will be a true vector image (no pixels, but instructions such as moveTo(), lineTo() and stroke()). Vector data has the advantage that it is resolution independent: you can zoom in and zoom out as much as you want, it will always be sharp.

This is explained in Chapter 10 of my book where you'll find the Barcodes example. In that chapter, you'll also discover the setFont() (or in iTextSharp the Font property). I quote from the API documentation:

public void setFont(BaseFont font)

Sets the text font.

Parameters:

font - the text font. Set to null to suppress any text

So if you don't want to see any text, you can add the following line to the above code snippet:

code25.Font = null;

Upvotes: 2

pid
pid

Reputation: 11607

You should avoid re-sizing by any means. The output is most likely pixel-perfect but when you scale it up/down a bilinear filter will smooth it rendering it blurry.

I had the same exact problem by embedding a QRC code in a PDF and solved it by avoiding a resize.

If you really need a different size apply it programmatically in code by using the correct interpolation algorithm.

Upvotes: 1

Related Questions