Problems With Specific Size Image Generation

I’m doing a proof-of-concept on switching to the Aspose barcode library, and I’m having some issues with generating images. We are printing the barcodes, but we aren’t using the direct Print() function because we are adding other information to the page. Therefore, we’re generating an image and then drawing it to the page.


We need the barcode to have a specific size, so we’re attempting to use it with AutoSize = False. But in this mode, the result seems to be a single black box, like the barcode was scaled far too large for the area. Does the barcode auto-scale to the image size, or do we also have to manually calculate the bar height and ratios to make it fit?

Example code (printing area is about 1.7" wide, so the rect.Width in the example is 170):

Using generator As New Aspose.BarCode.BarCodeBuilder()
generator.CodeText = Barcode.Barcode

If generator.CodeText.Length = 11 Then
generator.SymbologyType = Symbology.UPCA
ElseIf generator.CodeText.Length = 12 Then
generator.SymbologyType = Symbology.UPCA
generator.CodeText = generator.CodeText.Substring(0, 11)
Else
generator.SymbologyType = Symbology.Code39Standard
End If

generator.CodeLocation = CodeLocation.Below
generator.CodeTextAlignment = StringAlignment.Center
generator.CodeTextFont = Font.Clone()
generator.CodeTextColor = Color.Black

generator.AutoSize = False
generator.Resolution = New Resolution(e.Graphics.DpiX, e.Graphics.DpiY, ResolutionMode.Customized)
generator.GraphicsUnit = GraphicsUnit.Inch
generator.ImageHeight = rect.Height / 100
generator.ImageWidth = rect.Width / 100

Using img = generator.GenerateBarCodeImage()
e.Graphics.DrawImage(img, rect)
End Using
End Using

Thanks,
Brant
Hi Brant,

Thank you for contacting support. Please note that the barcode size also depends upon the code text. You can use Aspose.BarCode for .NET library which helps to get minimum sized image and then extend its size according to the requirement. Please go through the help topic: Generate Barcode Using Custom Width Support. The GetOnlyBarCodeImage method will return you the minimum possible size and then you can use GetCustomSizeBarCodeImage method to adjust its size. Please don't truncate its original width or height otherwise you could face recognition failure problems.

We hope, this helps. Please feel free to reply us in case of any confusion or questions.