接上:
五、三九码的制作方法
6.用C#操作 System.Drawing.Graphics画39码(code39)
(1)条形码的内容:string code = “123456789”,则验证码为2
通过函数code39_check_digit()得到验证码:
//得到验证码
private string code39_check_digit(string code)
{
int num2 = 0;
int num3 = Strings.Len(code);
for (int i = 1; i <= num3; i++)
{
num2 += this.code39_get_num(Strings.Mid(code, i, 1));
}
num2 = num2 % 0x2b;
return this.code39_get_char(num2);
}
然后在code前后加”*”号,(必须加星号读取条形码的设备才能正确识别):
code = "*" + code;
if (this.myAddCheckDigit)
{
code = code + this.code39_check_digit(code);
}
code = code + "*";
此时code="*1234567892*";
(2)把条码字元一个一个地转化为byte型(逻辑形式),并画出来
(请参阅<二>条形码之三九码(上)中39码编码对应表http://www.cnblogs.com/csui2008/archive/2009/01/07/csui2008.html)
int num10 = Strings.Len(code);
for (int i = 1; i <= num10; i++)
{
string expression = this.code39_get_pattern(Strings.Mid(code, i, 1));
if (expression != "")
{
float num6 = 0.0F;
float num7 = 0.0F;
float num9 = 0.0F;
int num11 = Strings.Len(expression);
int start = 1;
while (start <= num11)
{
float num8;
if (Strings.Mid(expression, start, 1) == "1")
{
num7 = X + (num5 * num2);
num6 = 2f * num2;
num9 = Y;
num8 = num;
num5 += 2;
}
else
{
num7 = X + (num5 * num2);
num6 = 1f * num2;
num9 = Y;
num8 = num;
num5++;
}
if ((start % 2) == 1)
{
ev.FillRectangle(brush, num7, num9, num6, num8);
}
start++;
}
num7 = X + (num5 * num2);
num5++;
}
}
(3)然后,确定是否要把条形码的内容显示在条形码下方,以下为显示条形码文本的程序段:
private void WriteNumber(string TITLE, float X, float Y,float X2,float Y2, Graphics ev)
{
Font font;
if (this.FontBold)
{
if (this.FontItalic)
{
font = new Font(this.myFontName, this.myFontSize, FontStyle.Italic | FontStyle.Bold);
}
else
{
font = new Font(this.myFontName, this.myFontSize, FontStyle.Bold);
}
}
else if (this.FontItalic)
{
font = new Font(this.myFontName, this.myFontSize, FontStyle.Italic);
}
else
{
font = new Font(this.myFontName, this.myFontSize);
}
SolidBrush brush2 = new SolidBrush(this.myFontColor);
SolidBrush brush = new SolidBrush(this.myFontBackGroundColor);
StringFormat format = new StringFormat();
if (this.myPrintChar)
{
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
SizeF ef = ev.MeasureString(TITLE, font);
float width = ef.Width;
float height = ef.Height;
ev.FillRectangle(brush, 0, Y - height, X2, height);
ev.DrawString(TITLE, font, brush2, X, Y - (height / 2f), format);
}
}
(4)条形码保存的类型:
/// <summary>
/// 保存条码的图片格式
/// </summary>
public enum SaveFileTypes
{
BitMap = 1,
Gif = 3,
Jpeg = 4,
Png = 5,
Tiff = 6
}
switch (this.SaveFileType)
{
case SaveFileTypes.BitMap:
if (Path.GetExtension(FileName) == "")
{
FileName = FileName + ".bmp";
}
image.Save(FileName, ImageFormat.Bmp);
return;
case SaveFileTypes.Gif:
if (Path.GetExtension(FileName) == "")
{
FileName = FileName + ".gif";
}
image.Save(FileName, ImageFormat.Gif);
return;
case SaveFileTypes.Jpeg:
if (Path.GetExtension(FileName) == "")
{
FileName = FileName + ".jpg";
}
image.Save(FileName, ImageFormat.Jpeg);
return;
case SaveFileTypes.Png:
if (Path.GetExtension(FileName) == "")
{
FileName = FileName + ".png";
}
image.Save(FileName, ImageFormat.Png);
return;
case SaveFileTypes.Tiff:
if (Path.GetExtension(FileName) == "")
{
FileName = FileName + ".tiff";
}
image.Save(FileName, ImageFormat.Tiff);
return;
}
运行结果: