一维码生成 c# winform GUI

时间:2024-04-09 08:37:02

最近看到同事小红在做一维码,感觉挺好玩,于是就在网上找了一个例子来玩玩。

下面的代码均为网上的代码,做了一些整理,但是忘记了出处,原作者看到可以提醒我,谢谢。
首先,一维码的相关知识可以先百度一下:http://baike.baidu.com/link?url=I4k1PM3WN5T9z0tGSC7eKLVg8GlDZvoWTM8N4cxzocueqAJBOHQl5SwB4fcpF3AWE3V8ipWqY_w7Rb0EcZA6v_
其中讲的还是比较清楚的,了解就行。
说明:本例讲的是code39,其他的没有哦。
界面如下:(不忍直视)
一维码生成 c# winform GUI
一维码生成 c# winform GUI
我使用微信扫一扫可以得到如下的结果:
一维码生成 c# winform GUI
一维码生成 c# winform GUI
应该说效果还是比较可以的。
如果需要该代码的就到360云盘去下吧:http://yunpan.cn/cKICBPBrk7xxh  提取码 0fe0
但是考虑到有些只是想看看流程的同学,我将主要的代码贴到下面了
        /// <summary>
/// 生成条码 Bitmap,自定义条码高度,自定义文字对齐样式
/// </summary>
/// <param name="sourceCode"></param>
/// <param name="barCodeHeight"></param>
/// <param name="sf"></param>
/// <returns></returns>
public Bitmap GetCode39(string sourceCode, int barCodeHeight, StringFormat sf)
{
BarCodeText = sourceCode.ToUpper();
int leftMargin = ;
int topMargin = ;
int thickLength = ;
int narrowLength = ;
int intSourceLength = sourceCode.Length;
string strEncode = "" ; //添加起始码“ *”.
var font = new System.Drawing.Font( "Segoe UI", );
string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*" ;
string[] Code39 =
{
/* 0 */ "" ,
/* 1 */ "" ,
/* 2 */ "" ,
/* 3 */ "" ,
/* 4 */ "" ,
/* 5 */ "" ,
/* 6 */ "" ,
/* 7 */ "" ,
/* 8 */ "" ,
/* 9 */ "" ,
/* A */ "" ,
/* B */ "" ,
/* C */ "" ,
/* D */ "" ,
/* E */ "" ,
/* F */ "" ,
/* G */ "" ,
/* H */ "" ,
/* I */ "" ,
/* J */ "" ,
/* K */ "" ,
/* L */ "" ,
/* M */ "" ,
/* N */ "" ,
/* O */ "" ,
/* P */ "" ,
/* Q */ "" ,
/* R */ "" ,
/* S */ "" ,
/* T */ "" ,
/* U */ "" ,
/* V */ "" ,
/* W */ "" ,
/* X */ "" ,
/* Y */ "" ,
/* Z */ "" ,
/* - */ "" ,
/* . */ "" ,
/*' '*/ "" ,
/* $ */ "" ,
/* / */ "" ,
/* + */ "" ,
/* % */ "" ,
/* * */ ""
};
sourceCode = sourceCode.ToUpper();
Bitmap objBitmap = new Bitmap(((thickLength * + narrowLength * ) * (intSourceLength + )) +
(leftMargin * ), barCodeHeight + (topMargin * ));
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle( Brushes.White, , , objBitmap.Width, objBitmap.Height);
for (int i = ; i < intSourceLength; i++)
{
//非法字符校验
if (AlphaBet.IndexOf(sourceCode[i]) == - || sourceCode[i] == '*' )
{
objGraphics.DrawString( "Invalid Bar Code", SystemFonts.DefaultFont, Brushes .Red, leftMargin, topMargin);
return objBitmap;
}
//编码
strEncode = string.Format("{0}0{1}" , strEncode,
Code39[AlphaBet.IndexOf(sourceCode[i])]);
}
strEncode = string.Format("{0}0010010100" , strEncode); //添加结束码“*”
int intEncodeLength = strEncode.Length;
int intBarWidth;
for (int i = ; i < intEncodeLength; i++) //绘制 Code39 barcode
{
intBarWidth = strEncode[i] == '' ? thickLength : narrowLength;
objGraphics.FillRectangle(i % == ? Brushes.Black : Brushes .White, leftMargin, topMargin, intBarWidth, barCodeHeight);
leftMargin += intBarWidth;
}
//绘制明码
Font barCodeTextFont = new Font( "黑体" , 10F);
RectangleF rect = new RectangleF(, barCodeHeight - , objBitmap.Width - , );
objGraphics.FillRectangle( Brushes.White, rect);
//文本对齐
objGraphics.DrawString(BarCodeText, barCodeTextFont, Brushes.Black, rect, sf);
return objBitmap;
}

好了,如果有兴趣的同学就可以去看看怎么实现自己的想要的一维码了。

说明:本人才疏学浅,文中必定有没有想到的地方,还请包涵。