条形码之三九码(下)

 

接上:

三九码的制作方法

   6.C#操作 System.Drawing.Graphics39(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;

 

            }

运行结果:

(以下为源码/Files/csui2008/BarCode39.rar)

转载于:https://www.cnblogs.com/csui2008/archive/2009/01/15/1376448.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值