barnhill / barcodelib Goto Github PK
View Code? Open in Web Editor NEWC# Barcode Image Generation Library
License: Apache License 2.0
C# Barcode Image Generation Library
License: Apache License 2.0
I would like to change the DPI from 96 to 300.
Is there any way to achieve this?
Hi
I've found this page on my travels through trying to get this to work.
Full disclosure, I'm not a developer!
I've managed to get a string to render in SSRS, but when the length goes over 30 characters, I get a little red X instead.
So, my question(s) are:
Any help would be greatly appreciated.
I work for Public Sector hospital, so being able to use this barcode to process patients a little faster would be fantastic.
Thanks
Dan
Dear Brad Barnhill:
Hello! I am Chun-Yen.I used your BarcodeLib open source since two years ago.Would you like me to solve my problem, please?.How can I set up the barcode's DPI?
Thank you very much!!!
Could be a .NET Standard / .NET Core library ?
Updated to version 2.0 the library throws error described in title.
Downgraded to version 1.3 it works as expected with no error.
Sample code:
var code = "85000923846235";
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
BarcodeLib.TYPE type = BarcodeLib.TYPE.Interleaved2of5;
b.IncludeLabel = true;
b.RotateFlipType = RotateFlipType.RotateNoneFlipNone;
b.AlternateLabel = code + CalculateCheckDigit(code);
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
b.BarWidth = null;
b.AspectRatio = null;
b.Encode(type, code, Color.Black, Color.White, 300, 150);
Method "CalculateCheckDigit" generates as the name said a one digit checksum.
This was previously in the assemblyinfo.cs:
[assembly:AllowPartiallyTrustedCallers]
From what I see on .Net Standard 2.0 this can just be added before the namespace.
I'm getting this error message from installing via nuget:
Severity Code Description Project File Line Suppression State
Error Could not install package 'BarcodeLib 2.2.5'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
Could someone help me install?
I have two bugs fond:
(RawData.StartsWith(AlternateLabel) || (AlternateLabel == null)
startWith go first after the null check
I cann't by past beter
Thanks
I have checked the Code128.cs file and on line 174, within the conditional statement part (Char.IsNumber(s[1]) || s[1] == Convert.ToChar(200))
, I believe that the s[1] == Convert.ToChar(200)
should also check the first position (s[0]
) in the event that the StartCharacter is not present.
If the StartCharacter is not present, and the barcode requires an FNC1 then the function should be in the first position.
Is there anyway to generate QR Codes with this library?
Oups please delete this !
I really loved your lib, but i can't make a multi line label, because i need more space to write information
By any chance is support for PDF417 barcodes going to be implemented at some point? This addition would help me get rid of another NuGet plugin.
Thanks!
I think it would be very beneficial to move the encoding of the barcode to its own public function so that you can get the actual binary value of the barcode without actually having to go through the whole generation process.
I am writing a library that interfaces with yours in order to render barcodes as dxf files(for AutoCad drawings). Returning the binary EncodedValue of the Barcode object is fine, but the Encode method runs through the whole image rendering process before setting the EncodedValue. (Line 481 of BarcodeLib.cs)
Moving the actual encoding of the barcode away from the image generation (which is on line 484) into its own public function (maybe called from the constructor) would allow this to happen without generating the image.
Here is a (working) example of the code.
public Barcode(string data, TYPE iType)
{
this.Raw_Data = data;
this.Encoded_Type = iType;
GenerateBarcode(); //Auto generate on this constructor
}
internal Image Encode()
{
ibarcode.Errors.Clear();
DateTime dtStartTime = DateTime.Now;
GenerateBarcode(); //Generation moved to its own function
this.Encoded_Value = ibarcode.Encoded_Value;
this.Raw_Data = ibarcode.RawData;
_Encoded_Image = (Image)Generate_Image();
this.EncodedImage.RotateFlip(this.RotateFlipType);
this.EncodingTime = ((TimeSpan)(DateTime.Now - dtStartTime)).TotalMilliseconds;
_XML = GetXML();
return EncodedImage;
}//Encode
public string GenerateBarcode(string raw_data = "") // The public generate function
{
if (raw_data != "")
{
Raw_Data = raw_data;
}
//make sure there is something to encode
if (Raw_Data.Trim() == "")
throw new Exception("EENCODE-1: Input data not allowed to be blank.");
if (this.EncodedType == TYPE.UNSPECIFIED)
throw new Exception("EENCODE-2: Symbology type not allowed to be unspecified.");
this.Encoded_Value = "";
this._Country_Assigning_Manufacturer_Code = "N/A";
switch (this.Encoded_Type)
{
case TYPE.UCC12:
case TYPE.UPCA: //Encode_UPCA();
ibarcode = new UPCA(Raw_Data);
break;
case TYPE.UCC13:
case TYPE.EAN13: //Encode_EAN13();
ibarcode = new EAN13(Raw_Data);
break;
case TYPE.Interleaved2of5_Mod10:
case TYPE.Interleaved2of5: //Encode_Interleaved2of5();
ibarcode = new Interleaved2of5(Raw_Data, Encoded_Type);
break;
case TYPE.Industrial2of5_Mod10:
case TYPE.Industrial2of5:
case TYPE.Standard2of5_Mod10:
case TYPE.Standard2of5: //Encode_Standard2of5();
ibarcode = new Standard2of5(Raw_Data, Encoded_Type);
break;
case TYPE.LOGMARS:
case TYPE.CODE39: //Encode_Code39();
ibarcode = new Code39(Raw_Data);
break;
case TYPE.CODE39Extended:
ibarcode = new Code39(Raw_Data, true);
break;
case TYPE.CODE39_Mod43:
ibarcode = new Code39(Raw_Data, false, true);
break;
case TYPE.Codabar: //Encode_Codabar();
ibarcode = new Codabar(Raw_Data);
break;
case TYPE.PostNet: //Encode_PostNet();
ibarcode = new Postnet(Raw_Data);
break;
case TYPE.ISBN:
case TYPE.BOOKLAND: //Encode_ISBN_Bookland();
ibarcode = new ISBN(Raw_Data);
break;
case TYPE.JAN13: //Encode_JAN13();
ibarcode = new JAN13(Raw_Data);
break;
case TYPE.UPC_SUPPLEMENTAL_2DIGIT: //Encode_UPCSupplemental_2();
ibarcode = new UPCSupplement2(Raw_Data);
break;
case TYPE.MSI_Mod10:
case TYPE.MSI_2Mod10:
case TYPE.MSI_Mod11:
case TYPE.MSI_Mod11_Mod10:
case TYPE.Modified_Plessey: //Encode_MSI();
ibarcode = new MSI(Raw_Data, Encoded_Type);
break;
case TYPE.UPC_SUPPLEMENTAL_5DIGIT: //Encode_UPCSupplemental_5();
ibarcode = new UPCSupplement5(Raw_Data);
break;
case TYPE.UPCE: //Encode_UPCE();
ibarcode = new UPCE(Raw_Data);
break;
case TYPE.EAN8: //Encode_EAN8();
ibarcode = new EAN8(Raw_Data);
break;
case TYPE.USD8:
case TYPE.CODE11: //Encode_Code11();
ibarcode = new Code11(Raw_Data);
break;
case TYPE.CODE128: //Encode_Code128();
ibarcode = new Code128(Raw_Data);
break;
case TYPE.CODE128A:
ibarcode = new Code128(Raw_Data, Code128.TYPES.A);
break;
case TYPE.CODE128B:
ibarcode = new Code128(Raw_Data, Code128.TYPES.B);
break;
case TYPE.CODE128C:
ibarcode = new Code128(Raw_Data, Code128.TYPES.C);
break;
case TYPE.ITF14:
ibarcode = new ITF14(Raw_Data);
break;
case TYPE.CODE93:
ibarcode = new Code93(Raw_Data);
break;
case TYPE.TELEPEN:
ibarcode = new Telepen(Raw_Data);
break;
case TYPE.FIM:
ibarcode = new FIM(Raw_Data);
break;
case TYPE.PHARMACODE:
ibarcode = new Pharmacode(Raw_Data);
break;
default: throw new Exception("EENCODE-2: Unsupported encoding type specified.");
}//switch
return this.Encoded_Value;
}
I can generate barcodes from visual studio, but when I deploy to IIS, it fails with some "Method not found" exception.
VS 2017, .NET 4.7.1, IIS 10
In looking at the old barcodelib embedded in our unshareable project code, it looks like we rely on the the old image sizing behavior documented in the ChangeLog at codeproject:
December 3, 2008
- Can now specify image size before encoding.
- Complete overhaul of the drawing function to allow for dynamic drawing size. Now calculates the bar width based on the specified image size. Removed resize functions due to inaccuracy of the resizing method. The encoding functions have been revamped to reflect these changes (removed some of the overloaded functions referencing the resize functions). Also removed a majority of the case statements from Generate_Image() to simplify it. Updated the article to reflect the changes to the Encode functions and the changes to the drawing functions.
There are a few things about how it looks that the current API works that are not nice here. First, to generate a barcode for an arbitrary string, the library’s user must be able to somehow predict the number of pixels necessary to render that barcode. This is really the biggest issue for me. I would like to be able to render a string and let the library automatically set BarcodeLib.Barcode.Width
to a high-enough value for me instead of getting an exception “EGENERATE_IMAGE-2: Image size specified not large enough to draw image. (Bar size determined to be less than 1 pixel)”.
To remedy this, I would like it to be possible to opt into the old behavior with an API like this:
/// <summary>If non-null, sets the width of a bar. <see cref="Width"/> is ignored and calculated automatically.</summary>
public int? BarWidth { get; set; }
/// <summary>If non-null, <see cref="Height"/> is ignored and set to <see cref="Width"/> divided by this value rounded down.</summary>
/// <remarks><para>As longer barcodes may be more difficult to align a scanner gun with,
/// growing the height based on the width automatically allows the gun to be rotated the
/// same amount regardless of how wide the barcode is. A recommended value is 2.
/// </para></remarks>
public double? AspectRatio { get; set; }
The idea is that you could choose to dynamically calculate the Width
, Height
, both, or neither. (Though “dynamically” calculating the Height
would basically be statically calculating it in the end ;-)). Does this API sound good? I think I’ll start working on a pull request, but thought I’d put this out here in case I get lost in some rabbit hole or others have comments/suggestions on this approach.
Hello,
For Types UPCA, and CODE128C bar codes, the numbers are not generated with the output Image object. Below is my code:
Barcode b = new Barcode();
Image img = b.Encode(TYPE.CODE128C, code, Color.Black, Color.White, 500, 130);
I would like to use this library in a com library i use but i need a strong name in the dll to invoke the function call.
Could you maybe add this or i could add a pull.
Hello,
Upong trying the library, the barcodes I get are always plain (without any data in them).
I tested it with the BarcodeLibTest project and also the Standard version (see Issue #32).
Code:
Barcode b = new Barcode();
Image img = b.Encode(TYPE.UPCA, "038000356216", Color.Black, Color.White, 290, 120);
img.Save("example.png", ImageFormat.Png);
Thanks.
Hi,
I am using BarcodeLib version 1.3.0 as my project framework is below 4.6.1.
I am not able to convert a number to the barcode of TYPE UPC-A. Though I can convert to another type like CODE-128.
Below is the error screenshot which I am getting.
Note: All parameters have values and nothing is null.
Also please find the stack trace which I got from exception block -
at System.String.StartsWith(String value)
at BarcodeLib.Barcode.Generate_Image()
at BarcodeLib.Barcode.Encode()
at BarcodeLib.Barcode.Encode(TYPE iType, String StringToEncode, Color ForeColor, Color BackColor, Int32 Width, Int32 Height)
at TagUpTool.Controllers.TagUpToolController.GetBarcode(String UPCNbr)
Awating your response.
is there a way for the label(text) to be clearer?, label(text) comes out pixilated
Thank you
Library is now strong named (which is great), but for use in SSRS reports it needs to now be marked with AllowPartiallyTrustedCallers attribute as discussed here:
Without this using the library in an RDL generates the error: "That assembly does not allow partially trusted callers"
I'm using the bardcodelib to generate barcodes within PDFs by RDLC-Designer.
If many people use our system at the same time, sometimes the PDF will be generated, but the barcode on it is missing and we got following error:
The definition of the report '' is invalid.
InnerException:
An unexpected error occurred in Report Processing.
InnerException:
Insufficient memory to continue the execution of the program.
Code snippet:
public byte[] GetBarCode39(string CodeNumber, int Lenght = 1000, int Height = 200, int FontSize = 40)
{
byte[] image = null;
try
{
MemoryStream ms = new MemoryStream();
Barcode barcode = new Barcode();
barcode.IncludeLabel = true;
barcode.Alignment = AlignmentPositions.CENTER;
barcode.LabelFont = new Font(FontFamily.GenericMonospace, FontSize, FontStyle.Regular);
var barcodeImage = barcode.Encode(TYPE.CODE39, CodeNumber, System.Drawing.Color.Black, System.Drawing.Color.White, Lenght, Height);
barcodeImage.Save(ms, ImageFormat.Jpeg);
image = ms.GetBuffer();
}
catch (Exception)
{
return null;
}
return image;
}
We already gave more power to the azure webservices, but this didn't solve the problem.. Do you know what's the problem?
Hello,
I'm working on generating barcodes in my C# application and for UPC-E codes I'm using BarcodeLib. Unfortunatelly I have noticed that barcodes aren't generated properly. I've tested few online generators and all of them are generating the same barcode when BarcodeLib generates different. Example below.
Barcode generated online:
Barcode generated by BarcodeLib:
As you can see even three first stripes aren't the same.
For generating barcode I'm using following code:
var barcode = new Barcode();
Image img = barcode.Encode(TYPE.UPCE, content, Color.Black, Color.White, 300, 150);
content is String containing number. In this example "425261".
Could you please give me some informations why codes aren't generated properly?
Thanks for any help
Hello,
When I generate a barcode of type EAN13 why is the label only displaying 12 digits ?
Thanks
Code-128C needs to support the FNC1 char as its valid as well as numeric values
How to Set Bar Code Density? Minimizing Barcode Width
When trying to build the .net core branch build and test app there is an error that the BarcodeLib reference in BarcordLibTest could not be found. Build fails to complete
Steps to re-create
download net-core-conversion branch with GitHub desktop
launch solution files with VS 2017 (15.8.8)
Build solution.
Receive the following errors
Attempting to reference the dll from \bin\debug results in different errors due to referenced version mismatch
Tried resolving issues but no luck :(
For many cases where barcodelib works as-is for developers, it would be great if it could be imported to a project via nuget. Providing it as-is encourages developers to copy/paste/maintain code that is not their own into their projects, which basically has the problems that come with bundling or statically linking against libraries.
So, if you could yourself provide a nuget package, endorse an existing one (if there is any), or would be willing to endorse a nuget package that a community member might make in the future, that’d be great.
Thanks!
Hello Brad,
First of all : I very appreciate your work!
I have made a little improvement to your barcode library, in order to show the EAN13 label in 'standard' format.
Since I'm newbie to GitHub, how can I submit you the code so you can check if it is a good improvement?
I tried to push the commit with VS2015 but I get back an error.
P.S. Forgive me for my bad english, it's not my main language..
Kind regards,
Daniele
Hello,
your example code generate barcode without label. When I set IncludeLabel property I get ArgumentNullException. I suppose that is because I have to set AlternateLabel but I do not understand why.
Is it a bug or a feature?
Thanks.
Would there be a way to use JSON as the encode-able string? Has anyone else tried that.
I've had some issues with compiling the information that I want encoded with under my barcode. What's the best way to do this? I just said json as it's a natural go-to for alot of things, and it seems to be popular in the php bunch of barcode apps and plugins. I'm building a client to take in a create barcodes. I'm reading them in Json format (although that may change). Basically just keeping a CRUD client and database of active vs inactive equipment in my company, and tracking by location and device types, printers, towers, etc. Basic IT stuff. My issue is using json, but it's mostly theoretical questioning than an issue with your code. Love it so far, just want to make it work for me if possible.
Hello, I've encountered this bug (i think it is) where any symbols in StringToEncode past 37th passed to method
barcode.Encode(TYPE.CODE128, "somelongtext", Color.Black, Color.White, 300, 150);
aren't displayed as symbols under the bars. The bars for these cut off numbers though are still generated.
here for example:
barcode.Encode(TYPE.CODE128, "236870469325449301543589144351218890486", Color.Black, Color.White, 300, 150);
results in following:
but without 2 last symbols it looks different:
I'd really want to keep these last 2 symbols still in the text under the bars. Can you please look into it?
Hi Brad,
i really like your Barcode Library, from all Libs we try´d, it works like a charm.
The only thing i need is what i have to add as an FNC1 Char.
In the Code128.cs Symbology file, i saw FNC1 is Char 200, so i tested it but it does not work for my Scanner when i try the following code:
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.IncludeLabel = true;
var value = String.Format("10L17146133619{0}0104000358091121", Convert.ToChar(200).ToString());
b.Encode(BarcodeLib.TYPE.CODE128, value, 10*65, 200);
b.SaveImage(sfd.FileName, savetype);
Is my Test Scanner (some Jartech) not configured properly or did i something wrong in my Barcode?
The value is legit so far and should read like:
Field: 10 Value: L17146133619 Length: 12 byte
Field: 01 Value: 04000358091121 Length: 14
The last field is OK, because it is static length, but field 10 is variable and should be terminated by the FNC1.
Maybe you have 2 Minutes to explain it.
And btw. when i activate the label text under it, the FNC1 char is printed in the label, maybe it is a bug or my fault, so i just wait what your answer is.
Regards
Thomas
Hello,
Is it possible to add support for GS1-128(EAN-128) code?
I have managed to solve this, a bit brutally, probably in the wrong way.
I am planning to make changes that will affect csproj and maybe the solution and then my brutal solution to this problem won't work.
I am thinking this has to depend on something in my environment that is wrong. Could it be me not having .Net core 2.0 installed?
Edit:
Installed .Net Core 3.
I tried (in PM console in VS 2019):
PM> Install-Package System.Drawing.Common -Version 4.0.0
It worked, installed stuff but then I get:
I checked what Install-Package did to the .csproj file, and found diff on this line:
..\packages\System.Drawing.Common.4.5.1\lib\net461\System.Drawing.Common.dll
Then I did:
PM> Install-Package System.Drawing.Common -Version 4.5.1
Didn't work either.
Then I reverted everything back. Just did "Install-Package System.Drawing.Common -Version 4.5.1" but I still get the same problem in BarcodeStandardExample.
What am I doing wrong?
Can we set the label text to something besides the string encoded in the barcode?
The string that shows up when IncludeLabel is set to true
Hello,
I tried your nice library and i was suprised that your example source code does not generate your example barcode (barcode with label). Why?? Is it any type of misunderstanding or incomplete example?
Thanks!
Hi, I have VS 2012 Express for Windows Applications but when I try and open the project it says it's not compatible. Do you have a new version of the files please?
Severity Code Description Project File Line Suppression State
Error Could not install package 'BarcodeLib 2.2.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
I also ran into the issue: #62
I found that the problem was not with the code but the project container.
By extracting and recomposing the code from this project into a single class file, I found that I could solve this issue and gain a couple of other valuable wins. No extra DLL to maintain and distribute, and smaller builds.
By a single class file I don't mean a single c# class, but rather a single .cs file structured with all the important pieces of your project, including the license. No code is changed, the project contents are simply shifted into a single file. I have created a secret gist here: https://gist.github.com/AnthonyVO/8c301818fc9ef86b809ef1ebe0630786
Creating this by hand would have been tiresome. It would also make it very difficult to see the differences between releases. To combat this I have created a relatively simple C# program to automate the process. Your project was the project that triggered the creation. I intend to push my utility to github shortly, I just need to I take some time to clean it up. My thought is that a tool like this could become part of a projects build process, creating a single file version of the project for those who desire it.
The Asks:
I am not asking you to restructure your code. The beauty of this is that you get to maintain this project as is.
I don't want to become the maintainer of single file versions of other peoples projects. This project is yours and the focus and credit should remain here. Therefore I am asking you to consider maintaining a single file version of this project. (Built automatically of course)
I am not asking for endorsement, at least not explicitly. I have already received some heat for this idea... See: https://stackoverflow.com/questions/57727349/combine-a-c-sharp-project-into-a-single-class-file
What I am asking for is permission to use your project and the gist as an example when I do the readme for my GitHub project.
Anthony
Hi,
I'm trying to generate a code-128 barcode using the library. The problem is that the barcode generated differs from the barcode generated from any other online tool I tried. Most of it is the same but there is one section that is different. I was able to read both barcodes using a free android barcode reader app and the data is the same. I'm a bit confused how two different code-128 barcodes can represent the same data. I tried different versions of code-128 but the generic code-128 is the closest to the online tools results.
A screen shot of the results is attached. The difference is marked in red. For the screen shot I used the test desktop application but I have the same results when using the library.
Regards.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.