. NET one-dimensional, QR code generation DEMO

Source: Internet
Author: User

Bar code technology is becoming more and more popular, and it is no longer limited to logistics, supermarkets and other professional industries. Many websites have already added QR codes. As a code customer guide, how can I not understand how to use this technology.

 

I found some information on the Internet. The source code generated by the one-dimensional code is relatively large and available, and there are also a lot of QR codes. However, I found that the source code is the same DEMO, and it cannot run, dizzy, and then find the source code of QrCodeNet to get involved.

 

The key code is as follows:

Generate one-dimensional code (call BarcodeLib ):

[Csharp]
// Generate a one-dimensional image
Private byte [] GetBarcode (int height, int width, BarcodeLib. TYPE type,
String code, out System. Drawing. Image image)
{
Image = null;
BarcodeLib. Barcode B = new BarcodeLib. Barcode ();
B. BackColor = System. Drawing. Color. White;
B. ForeColor = System. Drawing. Color. Black;
B. Required delabel = true;
B. Alignment = BarcodeLib. AlignmentPositions. CENTER;
B. LabelPosition = BarcodeLib. LabelPositions. bottoffenter;
B. ImageFormat = System. Drawing. Imaging. ImageFormat. Jpeg;
System. Drawing. Font font = new System. Drawing. Font ("verdana", 10f );
B. LabelFont = font;
 
B. Height = height;
B. Width = width;
 
Try
{
Image = B. Encode (type, code );
}
Catch (Exception e)
{
MessageBox. Show (e. Message );
Image = null;
}
// SaveImage (image, Guid. NewGuid (). ToString ("N") + ". png ");
Byte [] buffer = B. GetImageData (BarcodeLib. SaveTypes. GIF );
Return buffer;
}
 
// Call
Private void BuildBarcode ()
{
String number = string. IsNullOrEmpty (textBox1.Text. Trim ())? "Avx-(13614)-vR": textBox1.Text. Trim ();
BarcodeLib. TYPE codeType = comboBox1.SelectedIndex =-1? BarcodeLib. TYPE. CODE128: (BarcodeLib. TYPE) Enum. Parse (typeof (BarcodeLib. TYPE), comboBox1.Text );
System. Drawing. Image image;
Int width = 250, height = 100;
Byte [] buffer = GetBarcode (height, width,
CodeType, number, out image );
 
PictureBox1.Image = image;
If (image! = Null)
PictureBox1.SizeMode = image. Height> pictureBox1.Height? PictureBoxSizeMode. Zoom: PictureBoxSizeMode. Normal;
}

// Generate a one-dimensional image
Private byte [] GetBarcode (int height, int width, BarcodeLib. TYPE type,
String code, out System. Drawing. Image image)
{
Image = null;
BarcodeLib. Barcode B = new BarcodeLib. Barcode ();
B. BackColor = System. Drawing. Color. White;
B. ForeColor = System. Drawing. Color. Black;
B. Required delabel = true;
B. Alignment = BarcodeLib. AlignmentPositions. CENTER;
B. LabelPosition = BarcodeLib. LabelPositions. bottoffenter;
B. ImageFormat = System. Drawing. Imaging. ImageFormat. Jpeg;
System. Drawing. Font font = new System. Drawing. Font ("verdana", 10f );
B. LabelFont = font;

B. Height = height;
B. Width = width;

Try
{
Image = B. Encode (type, code );
}
Catch (Exception e)
{
MessageBox. Show (e. Message );
Image = null;
}
// SaveImage (image, Guid. NewGuid (). ToString ("N") + ". png ");
Byte [] buffer = B. GetImageData (BarcodeLib. SaveTypes. GIF );
Return buffer;
}

// Call
Private void BuildBarcode ()
{
String number = string. IsNullOrEmpty (textBox1.Text. Trim ())? "Avx-(13614)-vR": textBox1.Text. Trim ();
BarcodeLib. TYPE codeType = comboBox1.SelectedIndex =-1? BarcodeLib. TYPE. CODE128: (BarcodeLib. TYPE) Enum. Parse (typeof (BarcodeLib. TYPE), comboBox1.Text );
System. Drawing. Image image;
Int width = 250, height = 100;
Byte [] buffer = GetBarcode (height, width,
CodeType, number, out image );

PictureBox1.Image = image;
If (image! = Null)
PictureBox1.SizeMode = image. Height> pictureBox1.Height? PictureBoxSizeMode. Zoom: PictureBoxSizeMode. Normal;
}


QR code generation class (call QrCodeNet ):

[Csharp]
/// <Summary>
/// Class containing the description of the QR code and wrapping encoding and rendering.
/// </Summary>
Internal class CodeDescriptor
{
Public ErrorCorrectionLevel Ecl;
Public string Content;
Public QuietZoneModules QuietZones;
Public int ModuleSize;
Public BitMatrix Matrix;
Public string ContentType;
 
/// <Summary>
/// Parse QueryString that define the QR code properties
/// </Summary>
/// <Param name = "request"> HttpRequest containing http get data </param>
/// <Returns> a qr code descriptor object </returns>
Public static CodeDescriptor Init (ErrorCorrectionLevel, string content, QuietZoneModules qzModules, int moduleSize)
{
Var cp = new CodeDescriptor ();
 
/// Error correction level
Cp. Ecl = level;
/// Code content to encode
Cp. Content = content;
/// Size of the quiet zone
Cp. QuietZones = qzModules;
/// Module size
Cp. ModuleSize = moduleSize;
Return cp;
}
 
/// <Summary>
/// Encode the content with desired parameters and save the generated Matrix
/// </Summary>
/// <Returns> True if the encoding succeeded, false if the content is empty or too large to fit in a QR code </returns>
Public bool TryEncode ()
{
Var encoder = new QrEncoder (Ecl );
QrCode qr;
If (! Encoder. TryEncode (Content, out qr ))
Return false;
 
Matrix = qr. Matrix;
Return true;
}
 
/// <Summary>
/// Render the Matrix as a PNG image
/// </Summary>
/// <Param name = "ms"> MemoryStream to store the image bytes into </param>
Internal void Render (MemoryStream MS)
{
Var render = new GraphicsRenderer (new FixedModuleSize (ModuleSize, QuietZones ));
Render. WriteToStream (Matrix, System. Drawing. Imaging. ImageFormat. Png, MS );
ContentType = "image/png ";
}
}

/// <Summary>
/// Class containing the description of the QR code and wrapping encoding and rendering.
/// </Summary>
Internal class CodeDescriptor
{
Public ErrorCorrectionLevel Ecl;
Public string Content;
Public QuietZoneModules QuietZones;
Public int ModuleSize;
Public BitMatrix Matrix;
Public string ContentType;

/// <Summary>
/// Parse QueryString that define the QR code properties
/// </Summary>
/// <Param name = "request"> HttpRequest containing http get data </param>
/// <Returns> a qr code descriptor object </returns>
Public static CodeDescriptor Init (ErrorCorrectionLevel, string content, QuietZoneModules qzModules, int moduleSize)
{
Var cp = new CodeDescriptor ();

/// Error correction level
Cp. Ecl = level;
/// Code content to encode
Cp. Content = content;
/// Size of the quiet zone
Cp. QuietZones = qzModules;
/// Module size
Cp. ModuleSize = moduleSize;
Return cp;
}

/// <Summary>
/// Encode the content with desired parameters and save the generated Matrix
/// </Summary>
/// <Returns> True if the encoding succeeded, false if the content is empty or too large to fit in a QR code </returns>
Public bool TryEncode ()
{
Var encoder = new QrEncoder (Ecl );
QrCode qr;
If (! Encoder. TryEncode (Content, out qr ))
Return false;

Matrix = qr. Matrix;
Return true;
}

/// <Summary>
/// Render the Matrix as a PNG image
/// </Summary>
/// <Param name = "ms"> MemoryStream to store the image bytes into </param>
Internal void Render (MemoryStream MS)
{
Var render = new GraphicsRenderer (new FixedModuleSize (ModuleSize, QuietZones ));
Render. WriteToStream (Matrix, System. Drawing. Imaging. ImageFormat. Png, MS );
ContentType = "image/png ";
}
}
QR code generation call:

[Csharp]
String content = string. IsNullOrEmpty (textBox1.Text. Trim ())? "Http://www.nnbh.cn": textBox1.Text. Trim ();
 
Var codeParams = CodeDescriptor. Init (ErrorCorrectionLevel. H, content, QuietZoneModules. Two, 5 );
 
CodeParams. TryEncode ();
 
// Render the QR code as an image
Using (var MS = new MemoryStream ())
{
CodeParams. Render (MS );
 
Image image = Image. FromStream (MS );
PictureBox1.Image = image;
If (image! = Null)
PictureBox1.SizeMode = image. Height> pictureBox1.Height? PictureBoxSizeMode. Zoom: PictureBoxSizeMode. Normal;
}

String content = string. IsNullOrEmpty (textBox1.Text. Trim ())? "Http://www.nnbh.cn": textBox1.Text. Trim ();

Var codeParams = CodeDescriptor. Init (ErrorCorrectionLevel. H, content, QuietZoneModules. Two, 5 );

CodeParams. TryEncode ();

// Render the QR code as an image
Using (var MS = new MemoryStream ())
{
CodeParams. Render (MS );

Image image = Image. FromStream (MS );
PictureBox1.Image = image;
If (image! = Null)
PictureBox1.SizeMode = image. Height> pictureBox1.Height? PictureBoxSizeMode. Zoom: PictureBoxSizeMode. Normal;
}

 

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.