C # create a barcode

Source: Internet
Author: User
// Codeusing System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. drawing. imaging; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; using System. IO; namespace WindowsFormsApplication1 {public partial class Form1: Form {public Form1 () {InitializeComponent () ;} Bitmap B; private void Form1_Lo Ad (object sender, EventArgs e) // generate the QR code {} // <summary> // code39 code encoding scheme based on the string. The Width Adjustment Method is specific. The algorithm of code39 code is not studied, in short, the algorithms for each encoding scheme are different. // </summary> /// <param name = "strSource"> </param> /// <returns> </returns> private bitmap GetCode39 (string strSource) {int x = 5; int y = 0; int WidLength = 2; int NarrowLength = 1; int BarCodeHeight = 50; // The height of the barcode int intSourceLength = strSource. length; string strEncode = "010010" 100 "; string AlphaBet =" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -. $/+ % * "; // Code39 letter string [] Code39 = {/** // * 0 */" 000110100 ", /** // * 1 */"100100001",/** // * 2 */"001100001",/** // * 3 */"101100000 ", /** // * 4 */"000110001",/** // * 5 */"100110000",/** // * 6 */"001110000 ", /** // * 7 */"000100101",/** // * 8 */"100100100",/** // * 9 */"001100100 ", /** // * A */"100001001",/** // * B */"00100100 1 ",/** // * C */" 101001000 ",/** // * D */" 000011001 ", /** // * E */"100011000",/** // * F */"001011000",/** // * G */"000001101 ", /** // * H */"100001100",/** // * I */"001001100",/** // * J */"000011100 ", /** // * K */"100000011",/** // * L */"001000011",/** // * M */"101000010 ", /** // * N */"000010011",/** // * O */"100010010",/** // * P */"001010010 ", /** // * Q */"000000111",/** // * R */"10000011 0 ",/** // * S */" 001000110 ",/** // * T */" 000010110 ", /** // * U */"110000001",/** // * V */"011000001",/** // * W */"111000000 ", /** // ** X */"010010001",/** // * Y */"110010000",/** // * Z */"011010000 ", /** // *-*/"010000101 ",/**//*. */"110000100",/** // * ''' */"011000100",/** // * $*/"010101000 ", /** // */"010100010",/**/* + */"010001010",/**/* % */"000101010 ", /** // ***/"01001010 0 "}; strSource = strSource. toUpper (); // all input characters are converted into uppercase Bitmap objBitmap = new Bitmap (WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2 )) + (x * 2), BarCodeHeight + (y * 2); // specify the width and height of Graphics objGraphics = Graphics. fromImage (objBitmap); objGraphics. fillRectangle (Brushes. white, 0, 0, objBitmap. width, objBitmap. height); for (int I = 0; I <intSourceLength; I ++) {if (AlphaBet. indexOf (strSou Rce [I]) =-1 | strSource [I] = '*') {objGraphics. drawString ("contains invalid characters", SystemFonts. defaultFont, Brushes. red, x, y); return objBitmap;} strEncode = string. format ("{0} 0 {1}", strEncode, Code39 [AlphaBet. indexOf (strSource [I]);} strEncode = string. format ("{0} 0010010100", strEncode); int intEncodeLength = strEncode. length; int intBarWidth; // The width of the barcode for (int I = 0; I <intEncodeLength; I ++) {intBarW Idth = strEncode [I] = '1 '? WidLength: NarrowLength; objGraphics. FillRectangle (I % 2 = 0? Brushes. black: Brushes. white, x, y, intBarWidth, BarCodeHeight); // x + = intBarWidth;} return objBitmap;} private void textBox1_KeyPress (object sender, KeyPressEventArgs e) {if (e. keyChar = 13) {B = GetCode39 (this. textBox1.Text); this. pictureBox1.Image = B ;}} private void btnSave_Click (object sender, EventArgs e) {SaveFileDialog savefiledialog = new SaveFileDialog (); savefiledialog. filter = "image format (*. jpg) | *. jpg "; savefiledialog. title = "Save image"; savefiledialog. fileName = this. textBox1.Text; if (savefiledialog. showDialog () = DialogResult. OK) {B. save (savefiledialog. fileName, ImageFormat. jpeg );}}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.