Click here to Skip to main content
15,888,129 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following Barcode Image Generation Library code to generate barcodes.

Barcode.Encode(BarcodeLib.TYPE.BOOKLAND, Text, Width, Height) 


The generated images are all the same length, which distorts the barcode.

https://i.imgur.com/gHDNjzZ.png[^]

How do I get the barcodes to automatically scale properly?

What I have tried:

I have created an SSRS report that uses the BarcodeLib.dll assembly. It is working great except for the scaling of the barcodes. I know I am doing something stupid, but I have not been able to figure out what.
Posted
Updated 20-Oct-18 22:09pm
v4
Comments
Richard MacCutchan 21-Oct-18 4:05am    
You should check the documentation that came with the library. Also see below.

1 solution

When an image gets distorted it is often because one dimension is adjusted without the other. The key to keeping the correct aspect ratio while resizing an image is the algorithm used to calculate the ratio, viz.
NewHeight = GivenWidth * (OriginalHeight / OriginalWidth)

 or

NewWidth = GivenHeight * (OriginalWidth / OriginalHeight)

This calculation assumes that the “Given...” is the dimension the image should be resized to. Once we know this, we can multiply it by the original image’s aspect, and that will give us the other side's value we need. So, assuming the original image has a width of 1000 and a height of 1600 and we want it to be resized to a width of 500:

First find the aspect: (1600 / 1000) = aspect of 1.6
Now multiply the aspect by the desired new width: 1.6 * 500
The result of that multiplication is 800, which is what our height should be
In other words:

800 = 500 * (1600 / 1000)

So the resulting image would have a height of 800 and a width of 500.
 
Share this answer
 
Comments
Member 8525852 21-Oct-18 11:16am    
Are you replying to the specific project Barcode Image Generation Library...

https://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library

...or are you making a general observation about aspect ratio and resizing an image?

My specific problem is that you must supply a height and width to the function that generates the image. How do you know what the "original height/width" are? Ideally you would not supply height/width at all and the function would generate an image of the correct aspect ratio.

As far as checking the documentation, it doesn't seem to be addressed. The library demo application that comes with the code displays distorted images and there are text boxes to enter height and width. My guess is that you generate an image and then play with the height and width parameters to generate an image of the correct aspect ratio, but I wanted to make sure I was correct.
Richard MacCutchan 21-Oct-18 12:07pm    
When you have a problem with code that you got from a CodeProject article then you should post your question in the article's forum, so the person who wrote the code can help you.
Member 8525852 21-Oct-18 14:47pm    
I thought I was posting a question to the project. Evidently I wasn't. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900