How to create barcode image using c #?

I want to create a barcode image from an employee record using C #.

+4
source share
3 answers

Here is the code I used for a recent project. To do this, download and install the barcode barcode 39.

private static void CreateBarcode(string code) { var myBitmap = new Bitmap(500,50); var g = Graphics.FromImage(myBitmap); var jgpEncoder = GetEncoder(ImageFormat.Jpeg); g.Clear(Color.White); var strFormat = new StringFormat {Alignment = StringAlignment.Center}; g.DrawString(code, new Font("Free 3 of 9", 50), Brushes.Black, new RectangleF(0, 0, 500, 50), strFormat); var myEncoder = Encoder.Quality; var myEncoderParameters = new EncoderParameters(1); var myEncoderParameter = new EncoderParameter(myEncoder, 100L); myEncoderParameters.Param[0] = myEncoderParameter; myBitmap.Save(@"c:\Barcode.jpg", jgpEncoder, myEncoderParameters); } private static ImageCodecInfo GetEncoder(ImageFormat format) { var codecs = ImageCodecInfo.GetImageDecoders(); foreach (var codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } 

Hope this helps.

+10
source

A barcode is just a font. If you use a barcode font and specify a valid text value, the barcode will display correctly.

The barcode with which I had the most experience is 3 out of 9 barcodes . He had encodings for 0-9, AZ and several other characters. If you have a font for this barcode, you simply render the text (for example, "abc123") in that font and it will be encoded. Nothing more to do!

The question requires additional information to determine how you use the barcode.

0
source

Source: https://habr.com/ru/post/1341786/


All Articles