This article mainly introduces how to generate a Code39 barcode instance bar code gun by asp.net (C. For more information, see.
The Code is as follows: using System; using System. collections; using System. componentModel; using System. drawing; using System. data; namespace MSCL {// <summary> /// generate code 39 /// </summary> public class Code39 {private Hashtable m_Code39 = new Hashtable (); private byte m_Magnify = 0; // <summary> // magnification /// </summary> public byte Magnify {get {return m_Magnify ;} set {m_Magnify = value;} private int m_Heigh T = 40; // <summary> // graphic Height // </summary> public int Height {get {return m_Height;} set {m_Height = value ;}} private Font m_ViewFont = null; // <summary> // Font size // </summary> public Font ViewFont {get {return m_ViewFont ;} set {m_ViewFont = value ;}} public Code39 () {m_Code39.Add ("A", "1101010010110"); m_Code39.Add ("B", "1011010010110 "); m_Code39.Add ("C", "1101101001010"); m_Code 39. 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 ", & quot; 1010110100110 & quot;); m_Code39.A Dd ("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 ", & quot; 1100101101010 & quot;); m_Code39.Add (& quot (" 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 ", & quot; 1011001011010 & quot;); m_Code39.Add (& quot; + & quot ", "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 {// <summary> // basic category 1234567890ABC /// </summary> Code39Normal, /// <summary> // All ASCII Mode + A + B to indicate lower case /// </summary> Code39FullAscII} /// <summary> /// obtain the bar code image /// </summary> /// <param name = "p_Text"> text </param> // <param name = "p_Model"> Category </param> /// <param name = "p_StarChar"> whether to add the front and back signs * </param> // <returns> image </returns> public Bitmap GetCodeImage (string p_Text, code39Model p_Model, bool p_StarChar) {string _ ValueText = ""; string _ CodeText = ""; char [] _ ValueChar = null; switch (p_M Odel) {case Code39Model. Code39Normal: _ ValueText = p_Text.ToUpper (); break; default: _ ValueChar = p_Text.ToCharArray (); for (int I = 0; I! = _ ValueChar. length; I ++) {if (int) _ ValueChar [I]> = 97 & (int) _ ValueChar [I] <= 122) {_ ValueText + = "+" + _ ValueChar [I]. toString (). toUpper ();} else {_ ValueText + = _ ValueChar [I]. toString () ;}} break ;}_ ValueChar = _ ValueText. toCharArray (); if (p_StarChar = true) _ CodeText + = m_Code39 ["*"]; for (int I = 0; I! = _ ValueChar. length; I ++) {if (p_StarChar = true & _ ValueChar [I] = '*') throw new Exception ("the start symbol cannot appear *"); object _ CharCode = m_Code39 [_ ValueChar [I]. toString ()]; if (_ CharCode = null) throw new Exception ("unavailable character" + _ ValueChar [I]. toString (); _ CodeText + = _ CharCode. toString () ;}if (p_StarChar = true) _ CodeText + = m_Code39 ["*"]; Bitmap _ CodeBmp = GetImage (_ CodeText); GetViewImage (_ CodeBmp, p _ Text); return _ CodeBmp ;} /// <summary> /// draw the encoding image /// </summary> /// <param name = "p_Text"> encoding </param> /// <returns> graphics </returns> private Bitmap GetImage (string p_Text) {char [] _ Value = p_Text.ToCharArray (); // width = number of records to be drawn * magnification + Bitmap _ CodeImage = new Bitmap (_ Value. length * (int) m_Magnify + 1), (int) m_Height); Graphics _ Garphics = Graphics. fromImage (_ CodeImage); _ Garphics. fillRectangle (Bru Shes. White, new Rectangle (0, 0, _ CodeImage. Width, _ CodeImage. Height); int _ LenEx = 0; for (int I = 0; I! = _ Value. length; I ++) {int _ DrawWidth = m_Magnify + 1; if (_ Value [I] = '1') {_ Garphics. fillRectangle (Brushes. black, new Rectangle (_ LenEx, 0, _ DrawWidth, m_Height);} else {_ Garphics. fillRectangle (Brushes. white, new Rectangle (_ LenEx, 0, _ DrawWidth, m_Height);} _ LenEx + = _ DrawWidth;} _ Garphics. dispose (); return _ CodeImage;} // <summary> // draw text /// </summary> /// <param name = "p_CodeImage"> Figure </param> /// <param name = "p_Text"> text </param> private void GetViewImage (Bitmap p_CodeImage, string p_Text) {if (m_ViewFont = 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_CodeImage.Height-20) {_ Graphics. dispose (); return;} int _ StarHeight = p_C OdeImage. height-(int) _ FontSize. height; _ Graphics. fillRectangle (Brushes. white, new Rectangle (0, _ StarHeight, p_CodeImage.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: <% @ WebHandler Language = "C #" Class = "GetC39Handler" %> using System; Using System. web; using System. IO; using System. drawing; using System. drawing. imaging; public class GetC39Handler: IHttpHandler {// get the 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 _ CodeIma Ge = _ 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. R Esponse. clear (); context. response. bufferOutput = true; context. response. binaryWrite (_ Stream. getBuffer (); context. response. flush () ;}public bool IsReusable {get {return false ;}} code: // call the display order barcode Image1.ImageUrl = "GetC39Handler. ashx? Orderno. = 32134411 ";