asp.net (C #) generation of Code39 barcode instances

Source: Internet
Author: User
Tags bool tostring

  This article mainly introduces ASP.net (C #) to generate Code39 Barcode Instance Barcode gun can be scanned out. Need friends can come to the reference, I hope to help you.

  Code as follows: using System; Using System.Collections; Using System.ComponentModel; Using System.Drawing; Using System.Data;   namespace MscL {   ///<summary>    ///generate barcode Code39    ///</summary>     public class Code39     {      private Hashtable m_code39 = new Hashtable (); &nbsp ;         private byte m_magnify = 0;        ///<summary>        ///magnification multiple        ///</ summary>         Public byte magnify {get {return m_magnify;} set {m_magnify = value;}} &nbsp ;         private int m_height = 40;        ///<summary>        ///Graphics high        ///</s ummary>         public int Height {get {return m_height;} set {m_height = value;}}   &NB Sp       PrivaTe Font m_viewfont = null;        ///<summary>        ///font size        ///</ summary>         public Font ViewFont {get {return m_viewfont;} set {M_viewfont = value;}} &N Bsp             public Code39 ()         {              M_code39.add ("A", "1101010010110");             M_code39.add ("B", "1011010010110");             M_code39.add ("C", "1101101001010");             M_code39.add ("D", "1010110010110");             M_code39.add ("E", "1101011001010");             M_code39.add ("F", "1011011001010");             M_code39.add ("G", "1010100110110");             M_code39.add ("H", "1101010011010");             M_code39.add ("I", "1011010011010");             M_code39.add ("J", "1010110011010");             M_code39.add ("K", "1101010100110");             M_code39.add ("L", "1011010100110");             M_code39.add ("M", "1101101010010");             M_code39.add ("N", "1010110100110");             M_code39.add ("O", "1101011010010");             M_code39.add ("P", "1011011010010");             M_code39.add ("Q", "1010101100110");             M_code39.add ("R", "1101010110010");             M_code39.add ("S", "1011010110010");             M_code39.add ("T", "1010110110010");             M_code39.add ("U", "1100101010110 ");             M_code39.add ("V", "1001101010110");             M_code39.add ("W", "1100110101010");             M_code39.add ("X", "1001011010110");             M_code39.add ("Y", "1100101101010");             M_code39.add ("Z", "1001101101010");             M_code39.add ("0", "1010011011010");             M_code39.add ("1", "1101001010110");             M_code39.add ("2", "1011001010110");             M_code39.add ("3", "1101100101010");             M_code39.add ("4", "1010011010110");             M_code39.add ("5", "1101001101010");             M_code39.add ("6", "1011001101010");             M_code39.add ("7", "1010010110110");             M_code39.add ("8", "1101001011010");             M_code39.add ("9", "1011001011010");             M_code39.add ("+", "1001010010010");             M_code39.add ("-", "1001010110110");             M_code39.add ("*", "1001011011010");             M_code39.add ("/", "1001001010010");             M_code39.add ("%", "1010010010010");            //m_code39.add ("Contentquot;," 1001001001010 ");             M_code39.add (".", "1100101011010");             M_code39.add ("", "1001101011010");           {          public enum Code39model       &NBSP ; {           ///<SUMMARY&GT            ///basic category 1234567890ABC            ///</summar y>             Code39normal,            ///<summary&gt ;            ///full ASCII mode +a+b to represent lowercase            ///</summa ry>             CODE39FULLASCII                 <summary>        ///get barcode graphics        ///</summary>        ///<param name= "P_text" > Text information </param>        ///<param name= "P_model" & gt; category </param>        ///<param name= "P_starchar" > Increase before and after * number </param>        ///<returns> graphics </returns>         public Bitmap getcodeimage (String p_text, Code39model P_model,BOOL P_starchar)         {            String _valuetext = "";             String _codetext = "";             char[] _valuechar = null;             switch (P_model)             {    &NBSP ;           Code39model.code39normal:                     _valuetext = P_text.toupper ();                     break;                 default:                 & nbsp   _valuechar = P_text.tochararray ();                     for (int i = 0; I!= _valuechar.length i++)                     {        &nbsp               if ((int) _valuechar[i] >= (int) _valuechar[i] <= 122)                         {          nbsp                 _valuetext = "+" + _valuechar[i]. ToString (). ToUpper ();                          }         & nbsp               else                   &NBSP ;     (                            _valuete XT + _valuechar[i]. ToString ();                                   & nbsp        }                     Break                           _valuechar = _valuetext.tocha Rarray ();               if (P_starchar = = true) _codetext + = m_code39["*"];               for (int i = 0; I!= _valuechar.length; i++)             {                if (P_starchar = = True && _valuechar[i] = = ' * ') throw new Exception ("with a starting sign cannot appear *");                   Object _charcode = M_code39[_valuechar[i]. ToString ()];                 if (_charcode = null) throw new Exception ("Unavailable characters" + _valuechar[i ]. ToString ());                 _codetext + + _charcode.tostring ();                           if (P_starchar = = True) _code Text + + m_code39["*"];               Bitmap _codebmp = GetImage (_codetext);             Getviewimage (_codebmp, P_text);             return _codebmp;        }              ///<summary>       &N Bsp Draw coded graphics        ///</summary>        ///<param name= "P_text" > Code & lt;/param>        ///<returns> graphics </returns>         Private Bitmap GetImage (string p_text)         {            char[] _value = p_text.to Chararray ();              //wide = = need to draw number * magnification + two words wide            &NBS P   Bitmap _codeimage = new Bitmap (_value.length * (int) m_magnify + 1), (int) m_height);           &NBsp Graphics _garphics = Graphics.fromimage (_codeimage);               _garphics.fillrectangle (Brushes.white, New Rectangle (0, 0, _codeimage.wi DTH, _codeimage.height));               int _lenex = 0;             for (int i = 0; I!= _value.length i++)           &NBS P {                int _drawwidth = m_magnify + 1;         &NBS P       if (_value[i] = ' 1 ')                 {                    _garphics.fillrectangle (Brushes.black, New Rectangle (_lenex, 0, _drawwid Th, m_height));                   {                E LSE                 {          &NBsp         _garphics.fillrectangle (Brushes.white, New Rectangle (_lenex, 0, _drawwidth, m_height));                                 _lenex + = _drawwidth;                               _GARPHICS.DISPO SE ();             return _codeimage;        }        ///<summary>        ///drawing text        ///</summary>        ///<param name= "P_codeimage" > Graphics </param&gt ;        ///<param name= "P_text" > Text </param>         private void GETVI Ewimage (Bitmap p_codeimage, string p_text)         {            if (m_v Iewfont = = null) return;             Graphics _graphics = Graphics.fromimage (p_codeimage);             SizeF _fontsize = _graphics.measurestring (P_text, M_viewfont);               if (_fontsize.width > P_codeimage.width | | _fontsize.height > P_COD eimage.height-20)             {                _G Raphics. Dispose ();                 return;                         int _starheight = P_codeimage.height -(int) _fontsize.height;               _graphics.fillrectangle (Brushes.white, New Rectangle (0, _starheight, P_c Odeimage.width, (int) _fontsize.height));               int _starwidth = (p_codeimage.width-(int) _fontsize.width)/2;               _graphics.drawstring (P_text, M_viewfont, Brushes.black, _starwidth, _starheight);               _graphics.dispose ();           {   }   {    code as follows: <%@ WebHandler language= "C # class=" Ge Tc39handler "%>   using System; Using System.Web; Using System.IO; Using System.Drawing; Using System.Drawing.Imaging;   public class Getc39handler:ihttphandler {   //Get Code39 handler     public void ProcessRequest ( HttpContext context) {        String OrderNo = context. request.params["OrderNo"];         MSCL. Code39 _code39 = new MSCL. Code39 ();         _code39.height = 60;         _code39.magnify = 0;         _code39.viewfont = new Font ("Arial", 12);         System.Drawing.Image _codeimage = _code39.getcodeimage (OrderNo, MSCL. Code39.Code39Model.Code39Normal, True);         SYSTEM.IO.MEmorystream _stream = new System.IO.MemoryStream ();         _codeimage.save (_stream, System.Drawing.Imaging.ImageFormat.Jpeg);        //_codeimage.save (Server.MapPath ("/1.jpeg"));        //_codeimage.save (Server.MapPath ("/1.bmp"));        //_codeimage.save (Server.MapPath ("/1.gif"));           context. Response.ContentType = "Image/tiff";         context. Response.Clear ();         context. Response.bufferoutput = true;         context. Response.BinaryWrite (_stream.getbuffer ());         context. Response.Flush ();    }         public bool IsReusable {        get {    &NBSP ;       return false;        }    }}     code as follows:        //Call Display order Barcode     &N Bsp   Image1.IMAGEURL = "Getc39handler.ashx?" orderno=32134411 ";  
Related Article

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.