Home>

I saw some people write long codes about barcodes on the Internet,Some even sell it,So I checked the information,I hope it can be helpful to everyone.

principle My implementation principle is:

In fact, windows itself has a font for displaying barcodes.

Just change the number to this font to become a barcode.

Under the windows font library,There are eight fonts that can be used to convert numbers into barcodes:

Code39azaleanarrow1
Code39azaleanarrow2
Code39azaleanarrow3
Code39azalearegular1
Code39azalearegular2
Code39azaleawide1
Code39azaleawide2
Code39azaleawide3
Bitmap b=new bitmap (200,200);
Graphics g=graphics.fromimage (b);
Font font=new font ("code39azalearegular2", 32);
.G.drawstring ("123456", font, brushes.black, new pointf (100,100));
Picturebox1.backgroundimage=b;
Picturebox1.backgroundimagelayout=imagelayout.zoom.

Isn't it simple?Dear friends, I have given you ideas,When it is used in the project,The little friends can expand by themselves.

Attach an application from an example

using system;
using system.collections.generic;
using system.web;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;
using system.drawing.text;
using system.drawing.design;
using system.io;
using system.collections;
namespace public.equipment
{
 ///<summary>
 ///draw the barcode
 ///</summary>
 public class drawingbarcode
 {
 public drawingbarcode ()
 {
 //
 //todo:add constructor logic here
 //
 }
 #region Generate barcode from font
 ///<summary>
 ///draw pictures based on barcode
 ///</summary>
 ///<param name="strnumber">barcode</param>
 public void drawingbarcode (string strnumber)
 {
 privatefontcollection fonts=new privatefontcollection ();
 //39 with numbers
 //fonts.addfontfile (httpcontext.current.server.mappath (".") + "/barcodefonts/v100010_.ttf");
 //fontfamily ff=new fontfamily ("c39hrp48dltt", fonts);
 //39 yards
 strnumber="*" + strnumber + "*";
 fonts.addfontfile (httpcontext.current.server.mappath (".") + "/barcodefonts/free3of9x.ttf");
 fontfamily ff=new fontfamily ("free 3 of 9 extended", fonts);
 //close to barcode
 //fonts.addfontfile (httpcontext.current.server.mappath (".") + "/barcodefonts/v100014_.ttf");
 //fontfamily ff=new fontfamily ("c39p24dltt", fonts);
 font font=new font (ff, 12);
 //Set the image size
 image img=new bitmap (1, 1);
 graphics g=graphics.fromimage (img);
 sizef fontsize=g.measurestring (strnumber, font);
 int intwidth=convert.toint32 (fontsize.width);
 int intheight=convert.toint32 (fontsize.height);
 g.dispose ();
 img.dispose ();
 img=new bitmap (intwidth, intheight);
 g=graphics.fromimage (img);
 g.clear (color.white);
 g.compositingquality=compositingquality.highquality;
 g.smoothingmode=smoothingmode.highquality;
 g.drawstring (strnumber, font, brushes.black, 0, 0);
 memorystream stream=new memorystream ();
 img.save (stream, imageformat.jpeg);
 httpcontext.current.response.clearcontent ();
 httpcontext.current.response.contenttype="image/jpeg";
 httpcontext.current.response.binarywrite (stream.toarray ());
 g.dispose ();
 img.dispose ();
 }
 ///<summary>
 ///draw pictures based on barcode
 ///</summary>
 ///<param name="strnumber">barcode</param>
 ///<param name="intfontsize">font size</param>
 public void drawingbarcode (string strnumber, int intfontsize)
 {
 privatefontcollection fonts=new privatefontcollection ();
 //39 with numbers
 //fonts.addfontfile (httpcontext.current.server.mappath (".") + "/barcodefonts/v100010_.ttf");
 //fontfamily ff=new fontfamily ("c39hrp48dltt", fonts);
 //39 yards
 strnumber="*" + strnumber + "*";
 fonts.addfontfile (httpcontext.current.server.mappath (".") + "/barcodefonts/free3of9x.ttf");
 fontfamily ff=new fontfamily ("free 3 of 9 extended", fonts);
 //close to barcode
 //fonts.addfontfile (httpcontext.current.server.mappath (".") + "/barcodefonts/v100014_.ttf");
 //fontfamily ff=new fontfamily ("c39p24dltt", fonts);
 font font=new font (ff, intfontsize);
 //Set the image size
 image img=new bitmap (1, 1);
 graphics g=graphics.fromimage (img);
 sizef fontsize=g.measurestring (strnumber, font);
 int intwidth=convert.toint32 (fontsize.width);
 int intheight=convert.toint32 (fontsize.height);
 g.dispose ();
 img.dispose ();
 img=new bitmap (intwidth, intheight);
 g=graphics.fromimage (img);
 g.clear (color.white);
 g.compositingquality=compositingquality.highquality;
 g.smoothingmode=smoothingmode.highquality;
 g.drawstring (strnumber, font, brushes.black, 0, 0);
 memorystream stream=new memorystream ();
 img.save (stream, imageformat.jpeg);
 httpcontext.current.response.clearcontent ();
 httpcontext.current.response.contenttype="image/jpeg";
 httpcontext.current.response.binarywrite (stream.toarray ());
 g.dispose ();
 img.dispose ();
 }
 #endregion
 #region draw code39
 ///<summary>
 ///draw pictures based on barcode
 ///</summary>
 ///<param name="strnumber">barcode</param>
 public void drawingbarcode39 (string strnumber, int intfontsize, bool withstart)
 {
 viewfont=new font ("Songti", intfontsize);
 image img=getcodeimage (strnumber, code39model.code39normal, withstart);
 memorystream stream=new memorystream ();
 img.save (stream, imageformat.jpeg);
 httpcontext.current.response.clear ();
 httpcontext.current.response.clearcontent ();
 httpcontext.current.response.bufferoutput=true;
 httpcontext.current.response.contenttype="image/jpeg";
 httpcontext.current.response.binarywrite (stream.getbuffer ());
 httpcontext.current.response.flush ();
 }
 private hashtable m_code39=new hashtable ();
 private byte m_magnify=0;
 ///<summary>
 ///gain
 ///</summary>
 public byte magnify {get {return m_magnify;} set {m_magnify=value;}}
 private int m_height=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;}}
 /**
 The coding rules for code39 are:
 1. Every five lines represent a character;
 2. The thick line represents 1 and the thin line represents 0;
 3. The gap between the lines is 1 and the narrow is 0.
 4, five lines plus four gaps between them is a nine-bit binary code,And three of these nine bits must be 1, so it is called 39 yards;
 5. Each one at the beginning and end of the bar code * marks the start and end
 * /
 public drawingbarcode (boolean iscode39)
 {
 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 ("$", "1001001001010");
 m_code39.add (".", "1100101011010");
 m_code39.add ("", "1001101011010");
 }
 public enum code39model
 {
 ///<summary>
 ///basic category 1234567890abc
 ///</summary>
 code39normal, ///<summary>
 ///all ascii + a + b to indicate lower case
 ///</summary>
 code39fullascii
 }
 ///<summary>
 ///get bar code graphics
 ///</summary>
 ///<param name="p_text">text information</param>
 ///<param name="p_model">category</param>
 ///<param name="p_starchar">Whether to increase the 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)
 {
 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 ("Cannot appear with a start symbol *");
 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 coding graphics
 ///</summary>
 ///<param name="p_text">encoding</param>
 ///<returns>graphics</returns>
 private bitmap getimage (string p_text)
 {
 char [] _value=p_text.tochararray ();
 //width == the amount to draw * magnification + width of two words
 bitmap _codeimage=new bitmap (_value.length * ((int) m_magnify + 1), (int) m_height);
 graphics _garphics=graphics.fromimage (_codeimage);
 _garphics.fillrectangle (brushes.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,Text width greater than picture width will not be displayed
 ///</summary>
 ///<param name="p_codeimage">graphics</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_codeimage.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 ();
 }
 #endregion
 }
}
c
  • Previous IQueryable and IQueryProvider in C # Entity Framework
  • Next C # winform programming in response to the return key implementation code