生成一维码图片

 //生成一维码图片
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.IncludeLabel = true;
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
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;
}
 
//调用
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;
}
QQ:83199235
原文地址:https://www.cnblogs.com/softcg/p/6510977.html