Closed Strahan201 closed 1 year ago
You can set the BarWidth
property which causes the Width
property to be ignored and the width automatically calculated. For example, for the lowest possible width, you can set BarWidth
to 1
.
If you have a target maximum size which you want the barcode to expand to, you might still need a two-pass approach or loop increasing BarWidth
by 1
until you pass the limit and then go back one unless it is already the lowest valid value (1
).
Please ask if you need any further clarification.
If you set the width and height and leave the barwidth alone it will auto adjust to that size and make the barcode the maximum bar width that will fit in that size.
If you specify the Width, the effective bar width will automatically become the largest possible value. Or, if the Width is too small, you get the error that the bar width would have to be less than 1 pixel and you get the crash the OP wanted to avoid.
If you instead want the barcode to be the minimum width it can be without crashing, setting BarWidth to 1 will automatically calculate the minimum Width for you.
If you want to make your barcode fit into some presized designated area, then setting Width is the correct approach because overflowing would probably cause the barcode to be truncated, so you might as well get the exception. If you have an unbound space into which to put the barcode, then just setting BarWidth lets it grow as it needs. I guess that if you think the barcode is too small most of the time for BarWidth=1, you could render the barcode with BarWidth=1 to determine the minimum required size and then render it with Width=Math.Max(«target width», «the Width of the image from BarWidth=1»)
.
I could see making it easier to do the latter strategy by having the library just support it itself, but I think that with BarWidth you get the level of control you need already.
Hello. This isn't an issue per se, just a question. I have code where I am fixing the barcode width/height at 300/100. Worked fine for years, but I'm rewriting the app. I was making a GUI label editor and for value I had:
%productid%;%price%;%taxable%
There was a bug and instead of properly resolving those tokens, it tried to make a barcode with that as a literal string. It crashed saying that the barcode isn't big enough for the text, causing some barcode bars to be < 1 px. I fixed the bug, but it made me think about that issue. While I doubt I'll run into it for this, if in the future I want to encode that amount of text is there a way I can tell the library to use a dynamic width? Like a minimum of 300 but to automatically bump width up to the min required to achieve a valid barcode if 300 is too narrow?
My current solution is to set an int with min width then I build out the barcode in a try/catch. If the resultant barcode is too narrow, it trips the catch and in that it adds 100 to the min width then tries again. It works, but it's hacky as hell. What is the right way to handle the sizing issue?