Generate Barcode Code

Source: Internet
Author: User

This code is for generating barcode code

Build effect

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Drawing;

Namespace Paabo.WordProcessing.Common
{
public class Barcodeprovider
{
#region Single case
private static Barcodeprovider _intance;
public static Barcodeprovider Intance
{
Get
{
if (_intance = null)
{
_intance = new Barcodeprovider ();
}
return _intance;
}
}
#endregion

#region Size
<summary>
Picture width
</summary>
private int _width = 200;
public int Width
{
get {return _width;}
set {_width = value;}
}


<summary>
Picture height
</summary>
private int _height = 80;
public int Height
{
get {return _height;}
set {_height = value;}
}

<summary>
Clear text Height
</summary>
private int _textheight = 25;
public int TextHeight
{
get {return _textheight;}
set {_textheight = value;}
}

#endregion

#region margins
private int _margin_top = 5;

<summary>
Top margin
</summary>
public int Margin_top
{
get {return _margin_top;}
set {_margin_top = value;}
}

private int _margin_left = 5;

<summary>
Left margin
</summary>
public int Margin_left
{
get {return _margin_left;}
set {_margin_left = value;}
}


private int _margin_right = 5;

<summary>
Right margin
</summary>
public int Margin_right
{
get {return _margin_right;}
set {_margin_right = value;}
}

private int _margin_bottom = 5;
<summary>
Bottom margin
</summary>
public int Margin_bottom
{
get {return _margin_bottom;}
set {_margin_bottom = value;}
}
#endregion

Private font _textfont = new Font ("Song Body", 12);

<summary>
Clear Text font
</summary>
Public Font Textfont
{
get {return _textfont;}
set {_textfont = value;}
}

Private Pen _blackpen = new Pen (brushes.black);
Private Pen _whitepen = new Pen (brushes.white);

<summary>
Replace clear text with encoding
</summary>
<param name= "Text" > Clear text content </param>
<returns> Coding </returns>
private string Converttobarcode (string text)
{
String code = String. Empty;
foreach (char item in text)
{
int itemvalue = Item;
Code + + convert.tostring (Itemvalue, 2) + ",";
}
return code;
}

<summary>
Generate a bar picture of a string
</summary>
<param name= "Text" > Clear text content </param>
<returns></returns>
Public Bitmap createbarcodeimage (string text)
{
Bitmap map = new Bitmap (Width, Height);
Graphics g = graphics.fromimage (map);
Try
{
String code = Converttobarcode (text);
Code = string. Format ("101{0}101", code);
char[] Array = code. ToCharArray ();
char[] Textarray = text. ToCharArray ();
int linewidth = (width-margin_left-margin_right)/(array. Length-textarray.length);
int lineheight = Height-textheight-margin_bottom;

_blackpen.width = linewidth;
_whitepen.width = linewidth;

int x = 5;
int topy = 5;
int bottony = Height-margin_bottom-textheight;

int index = 0;
char pitem = ';
Pen pen = null;
foreach (char item in array)
{

if (item = = ', ')
{
                    &n

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.