Asp.net (C #) to generate a barcode

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Response. Write (bar_code ("www.sosuo8.com", 100, 1, 2 ));
}
Public String bar_code (Object STR, int CH, int CW, int type_code)
{
// STR: input string; Ch: height of the barcode to be displayed; CW: width of the barcode to be displayed; type_code:CodeType
String strtmp = Str. tostring ();
String code = strtmp;
// Tolower () converts string to a lowercase copy, and returns a case-sensitive rule using the specified region.
Strtmp = strtmp. tolower ();
Int Height = CH;
Int width = CW;

// Convert the input parameters.
Strtmp = strtmp. Replace ("0", "_ | _ |__ | _ | ");;
Strtmp = strtmp. Replace ("1", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("2", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("3", "_ | |__ | _ | ");
Strtmp = strtmp. Replace ("4", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("5", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("7", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("6", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("8", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("9", "_ | |__ | _ | ");
Strtmp = strtmp. Replace ("A", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("B", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("C", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("D", "_ | |__ | _ | ");
Strtmp = strtmp. Replace ("e", "_ | |__ | _ | ");
Strtmp = strtmp. Replace ("F", "_ | |__ | _ | ");
Strtmp = strtmp. Replace ("g", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("H", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("I", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("J", "_ | _ |__ | _ | ");
Strtmp = strtmp. Replace ("k", "_ | _ |__ | ");
Strtmp = strtmp. Replace ("L", "_ | _ |__ | ");
Strtmp = strtmp. Replace ("M", "_ | _ |__ | ");
Strtmp = strtmp. Replace ("N", "_ | _ |__ | ");
Strtmp = strtmp. Replace ("O", "_ | _ |__ | ");
Strtmp = strtmp. Replace ("P", "_ | _ |__ | ");
Strtmp = strtmp. Replace ("r", "_ | |__ | ");
Strtmp = strtmp. Replace ("Q", "_ | |__ | ");
Strtmp = strtmp. Replace ("S", "_ | |__ | ");
Strtmp = strtmp. Replace ("T", "_ | |__ | ");
Strtmp = strtmp. Replace ("u", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("V", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("W", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("X", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("Y", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("Z", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("-", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("*", "_ |__ | _ | ");
Strtmp = strtmp. Replace ("/", "_ |__ | ");
Strtmp = strtmp. Replace ("%", "_ | _ |__ | ");
Strtmp = strtmp. Replace ("+", "_ |__ | ");
Strtmp = strtmp. Replace (".", "_ |__ | _ | ");
Strtmp = strtmp. replace ("_", "<span style = 'height:" + height + "; width:" + width + "; Background: # ffffff; '> </span> ");
Strtmp = strtmp. replace ("|", "<span style = 'height:" + height + "; width:" + width + "; Background: #000000; '> </span> ");

If (type_code = 1)
{
Return strtmp + "<br>" + code;
}
Else
{
Return strtmp;
}
}
}

Method 2:
Using system. drawing;

Public void createcodelogo (string code)
{

Long Len = code. length;
String laststring = "";
Char [] list = new char [Len + 1];

List = code. tochararray ();

For (INT I = 0; I <list. length; I ++)
{
Laststring + = This. converttobinarystring (list [I]. tostring ());
}

Char [] numlist = new char [laststring. Length + 1];
Numlist = laststring. tochararray ();

Bitmap image = new Bitmap (200,140 );
Graphics G = graphics. fromimage (image );

G. Clear (color. White );

Pen penblack = new pen (color. fromargb (255, 0, 0, 0), 2.5f );
Pen penwhite = new pen (color. White, 2.5f );

Int J = 0;

For (float K = 10; j <numlist. length; k + = 2f, J ++)
{
If (numlist [J]. tostring () = "1 ")
{
G. drawline (penblack, K, 10, K, 110 );

}
Else
{
G. drawline (penwhite, K, 10, K, 110 );
}

If (J % 4 = 0)
{
G. drawstring (list [J/4]. tostring (), new system. drawing. font ("Courier New", 12), new solidbrush (color. red), K, 112 );
}
}
Image. Save (response. outputstream, system. Drawing. imaging. imageformat. GIF );
}

// Convert a string value to a binary string value
Public String converttobinarystring (string BUF)
{
Int [] temp = new int [20];
String binary;
Int val = 0, I = 0, J;

// Convert the character to a decimal number first
Try
{
Val = convert. toint32 (BUF );
}
Catch
{
Val = 0;
}

If (val = 0)
{
Return ("0000 ");
}

I = 0;
While (Val! = 0)
{
Temp [I ++] = Val % 2;
Val/= 2;
}

Binary = "";

For (j = 0; j <= I-1; j ++)
{
Binary ++ = (char) (temp [I-j-1] + 48 );
}

If (Binary. Length <4) // if it is less than 4 digits, the left side of the complement is zero.
{
Int Len = 4-binary. length;
String STR = "";

While (LEN> 0)
{
STR + = "0 ";
Len --;
}

Binary = STR + binary;
}

Return (Binary );
}

Protected void button#click (Object sender, eventargs E)
{
Createcodelogo (textbox1.text );
}

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.