On the internet to see some people write about the barcode code is very long, some even to sell, so check the information, hope to be helpful to everyone.
The principle of my implementation is:
In fact, Windows itself has a font that is used to display barcodes.
As long as you change the number to this font it becomes a barcode.
Under the Windows Font Library, there are eight types of fonts that can be used to convert numbers to barcodes:
The code is as follows:
Code39azaleanarrow1
Code39azaleanarrow2
Code39azaleanarrow3
Code39azalearegular1
Code39azalearegular2
Code39azaleawide1
Code39azaleawide2
Code39azaleawide3
Paste the code for your reference:
The code is as follows:
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.
is not very simple, pro-people, thinking to everyone, the specific use of the project, the small partners themselves to expand the next.
Also attached is an example of the application
The code is as follows:
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>
Drawing barcodes
</summary>
public class Drawingbarcode
{
Public Drawingbarcode ()
{
//
TODO: Add constructor logic here
//
}
#region generate barcodes based on fonts
<summary>
Drawing pictures based on barcodes
</summary>
<param name= "Strnumber" > Barcode </param>
public void Drawingbarcode (string strnumber)
{
privatefontcollection fonts = new PrivateFontCollection ();
39 with Digital
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 barcodes
Fonts. Addfontfile (HttpContext.Current.Server.MapPath (".") + "/barcodefonts/v100014_. TTF ");
FontFamily ff = new FontFamily ("C39P24DLTT", fonts);
Font font = new Font (FF, 12);
Set Picture 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>
Drawing pictures based on barcodes
</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 Digital
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 barcodes
Fonts. Addfontfile (HttpContext.Current.Server.MapPath (".") + "/barcodefonts/v100014_. TTF ");
FontFamily ff = new FontFamily ("C39P24DLTT", fonts);
Font font = new Font (FF, intfontsize);
Set Picture 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 Code
<summary>
Drawing pictures based on barcodes
</summary>
<param name= "Strnumber" > Barcode </param>
public void DrawingBarCode39 (string strnumber,int intfontsize,bool Withstart)
{
ViewFont = new Font ("Song Body", 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>
Magnification
</summary>
Public byte magnify {get {return m_magnify;} set {m_magnify = value;}}
private int m_height = 40;
<summary>
Graphics high
</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 codes are:
1, each five line represents a character;
2, The thick line is 1, the thin wire represents 0;
3, the gap between lines of the width of the expression 1, a narrow representation of 0;
4, five lines plus the four gap between them is nine-bit binary encoding, and this nine bit must have three bits is 1, so called 39 yards;
5, the first and last of the bar code * Logo 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>
Full ASCII method +a+b to indicate lowercase
</summary>
Code39fullascii
}
<summary>
Get barcode Graphics
</summary>
<param name= "P_text" > Text messages </param>
<param name= "P_model" > Categories </param>
<param name= "P_starchar" > whether to add before and after * # </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] >= && (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 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>
Drawing encoded graphics
</summary>
<param name= "P_text" > Coding </param>
<returns> Graphics </returns>
Private Bitmap GetImage (string p_text)
{
char[] _value = P_text.tochararray ();
width = = Number to be drawn * magnification + two characters wide
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
}
}
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
The most concise C # generation Barcode picture ideas and sample sharing in history
This address: http://www.paobuke.com/develop/c-develop/pbk23204.html
Related content Implementation of C # WinForm login Box Verification Code C # the way to connect to MySQL "based on vs2010" on the use of the C # Express expression. Net3£??? Êìa?? C#? Dμ?? ˉíd
C # overloaded Equality (= =) Operator Example C # method of reading a specified line from a file or standard input device C # Conversion of dictionary to XML file C # multiplication sign usage when calling SQL statements
The most concise C # generation Barcode picture ideas and sample sharing