1,下面一个类,可以直接复制下去使用;

public class Code39
    {
        private Hashtable m_Code39 = new Hashtable();
        private byte m_Magnify = 0;
        /// <summary>
        /// 放大倍数
        /// </summary>
        public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }

private int m_Height = 40;
        /// <summary>
        /// 图形高
        /// </summary>
        public int Height { get { return m_Height; } set { m_Height = value; } }

private Font m_ViewFont = null;
        /// <summary>
        /// 字体大小
        /// </summary>
        public Font ViewFont { get { return m_ViewFont; } set { m_ViewFont = value; } }

public Code39()
        {

m_Code39.Add("A", "1101010010110");
            m_Code39.Add("B", "1011010010110");
            m_Code39.Add("C", "1101101001010");
            m_Code39.Add("D", "1010110010110");
            m_Code39.Add("E", "1101011001010");
            m_Code39.Add("F", "1011011001010");
            m_Code39.Add("G", "1010100110110");
            m_Code39.Add("H", "1101010011010");
            m_Code39.Add("I", "1011010011010");
            m_Code39.Add("J", "1010110011010");
            m_Code39.Add("K", "1101010100110");
            m_Code39.Add("L", "1011010100110");
            m_Code39.Add("M", "1101101010010");
            m_Code39.Add("N", "1010110100110");
            m_Code39.Add("O", "1101011010010");
            m_Code39.Add("P", "1011011010010");
            m_Code39.Add("Q", "1010101100110");
            m_Code39.Add("R", "1101010110010");
            m_Code39.Add("S", "1011010110010");
            m_Code39.Add("T", "1010110110010");
            m_Code39.Add("U", "1100101010110");
            m_Code39.Add("V", "1001101010110");
            m_Code39.Add("W", "1100110101010");
            m_Code39.Add("X", "1001011010110");
            m_Code39.Add("Y", "1100101101010");
            m_Code39.Add("Z", "1001101101010");
            m_Code39.Add("0", "1010011011010");
            m_Code39.Add("1", "1101001010110");
            m_Code39.Add("2", "1011001010110");
            m_Code39.Add("3", "1101100101010");
            m_Code39.Add("4", "1010011010110");
            m_Code39.Add("5", "1101001101010");
            m_Code39.Add("6", "1011001101010");
            m_Code39.Add("7", "1010010110110");
            m_Code39.Add("8", "1101001011010");
            m_Code39.Add("9", "1011001011010");
            m_Code39.Add("+", "1001010010010");
            m_Code39.Add("-", "1001010110110");
            m_Code39.Add("*", "1001011011010");
            m_Code39.Add("/", "1001001010010");
            m_Code39.Add("%", "1010010010010");
            m_Code39.Add("$", "1001001001010");
            m_Code39.Add(".", "1100101011010");
            m_Code39.Add(" ", "1001101011010");
        }

public enum Code39Model
        {
            /// <summary>
            /// 基本类别 1234567890ABC
            /// </summary>
            Code39Normal,
            /// <summary>
            /// 全ASCII方式 +A+B 来表示小写
            /// </summary>
            Code39FullAscII
        }
        /// <summary>
        /// 获得条码图形
        /// </summary>
        /// <param name="p_Text">文字信息</param>
        /// <param name="p_Model">类别</param>
        /// <param name="p_StarChar">是否增加前后*号</param>
        /// <returns>图形</returns>
        public Bitmap GetCodeImage(string p_Text, Code39Model p_Model, bool p_StarChar)
        {
            string _ValueText = "";
            string _CodeText = "";
            char[] _ValueChar = null;
            switch (p_Model)
            {
                case Code39Model.Code39Normal:
                    _ValueText = p_Text.ToUpper();
                    break;
                default:
                    _ValueChar = p_Text.ToCharArray();
                    for (int i = 0; i != _ValueChar.Length; i++)
                    {
                        if ((int)_ValueChar[i] >= 97 && (int)_ValueChar[i] <= 122)
                        {
                            _ValueText += "+" + _ValueChar[i].ToString().ToUpper();

}
                        else
                        {
                            _ValueText += _ValueChar[i].ToString();
                        }
                    }
                    break;
            }
            _ValueChar = _ValueText.ToCharArray();
            if (p_StarChar == true) _CodeText += m_Code39["*"];
            for (int i = 0; i != _ValueChar.Length; i++)
            {
                if (p_StarChar == true && _ValueChar[i] == '*') throw new Exception("带有起始符号不能出现*");
                object _CharCode = m_Code39[_ValueChar[i].ToString()];
                if (_CharCode == null) throw new Exception("不可用的字符" + _ValueChar[i].ToString());
                _CodeText += _CharCode.ToString();
            }
            if (p_StarChar == true) _CodeText += m_Code39["*"];

Bitmap _CodeBmp = GetImage(_CodeText);
            GetViewImage(_CodeBmp, p_Text);
            return _CodeBmp;
        }

/// <summary>
        /// 绘制编码图形
        /// </summary>
        /// <param name="p_Text">编码</param>
        /// <returns>图形</returns>
        private Bitmap GetImage(string p_Text)
        {
            char[] _Value = p_Text.ToCharArray();

//宽 == 需要绘制的数量*放大倍数 + 两个字的宽   
            Bitmap _CodeImage = new Bitmap(_Value.Length * ((int)m_Magnify + 1), (int)m_Height);
            Graphics _Garphics = Graphics.FromImage(_CodeImage);
            _Garphics.FillRectangle(Brushes.White, new Rectangle(0, 0, _CodeImage.Width, _CodeImage.Height));

int _LenEx = 0;
            for (int i = 0; i != _Value.Length; i++)
            {
                int _DrawWidth = m_Magnify + 1;
                if (_Value[i] == '1')
                {
                    _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _DrawWidth, m_Height));
                }
                else
                {
                    _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _DrawWidth, m_Height));
                }
                _LenEx += _DrawWidth;
            }

_Garphics.Dispose();
            return _CodeImage;
        }
        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name="p_CodeImage">图形</param>
        /// <param name="p_Text">文字</param>
        private void GetViewImage(Bitmap p_CodeImage, string p_Text)
        {
            if (m_ViewFont == null) return;
            Graphics _Graphics = Graphics.FromImage(p_CodeImage);
            SizeF _FontSize = _Graphics.MeasureString(p_Text, m_ViewFont);
            if (_FontSize.Width > p_CodeImage.Width || _FontSize.Height > p_CodeImage.Height - 20)
            {
                _Graphics.Dispose();
                return;
            }
            int _StarHeight = p_CodeImage.Height - (int)_FontSize.Height;
            _Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarHeight, p_CodeImage.Width, (int)_FontSize.Height));
            int _StarWidth = (p_CodeImage.Width - (int)_FontSize.Width) / 2;
            _Graphics.DrawString(p_Text, m_ViewFont, Brushes.Black, _StarWidth, _StarHeight);
            _Graphics.Dispose();
        }

}

2,如何使用?

Code39 _Code39 = new Code39();
            _Code39.Height = 120; //高度
            _Code39.Magnify = 1;  //放大倍数
            _Code39.ViewFont = new Font("宋体", 20);  // 设置字体

var bitMap = _Code39.GetCodeImage("6901028223751", Code39.Code39Model.Code39Normal, true);  // 根据传入的参数,生成对应的“位图”;
           bitMap.Save( @"D:\barcode.png", System.Drawing.Imaging.ImageFormat.Jpeg);   // 将“位图”保存到本地路径;

.Net 生成条形码的更多相关文章

  1. C# 在Word文档中生成条形码

    C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...

  2. 使用html2canvas实现批量生成条形码

    /*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...

  3. JAVA生成条形码

    1.下载生成条形码所需要的jar包barcode4j.jar: 2.java生成条形码代码 import java.awt.image.BufferedImage;import java.io.Fil ...

  4. C# 生成条形码

    原文地址:http://www.cnblogs.com/xcsn/p/4514759.html 引用BarcodeLib.dll(百度云中有)生成条形 protected void Button2_C ...

  5. C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)

    原文:http://blog.csdn.net/kongwei521/article/details/17588825 首先效果: 一.下载BarcodeLib.dll 下载地址 :http://do ...

  6. PHP5生成条形码器

    前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...

  7. PHP生成条形码

    前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...

  8. C# 利用BarcodeLib.dll生成条形码

    首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...

  9. 使用PHP-Barcode轻松生成条形码(一)

    最近由于工作需要,研究了一下PHP如何生成条形码.虽然二维码时下比较流行,但是条形码依然应用广泛,不可替代.园子里有很多讲利用PHP生成条形码的文章,基本上都是围绕Barcode Bakery的,它虽 ...

  10. java 生成条形码

    package com.sun.erwei; import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;impo ...

随机推荐

  1. js 图片点击放大功能

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. CTP API开发期货自动交易平台概论

    题目比较小众,先介绍一下CTP. 综合交易平台CTP(Comprehensive Transaction Platform)是由上海期货信息技术有限公司(上海期货交易所的全资子公司)开发的期货交易平台 ...

  3. DDD实践(一)

    DDD实践切入点(一) 前两篇:大型系统的支撑,应用系统开发思想的变迁 之前大致说了使用DDD的前期准备,现在可以真正开始实践了,以我刚刚结束的一个简单的经典DDD方式的项目为例子,当然由于比较简单, ...

  4. java 生成easyui 所需要的森林

    在项目中,可能会遇到机构树这种格式,但是数据库存储的数据 不能维护这样子的树,所以需要中间转换来完成,zTree可以支持pid,id,name的格式,但是easyui貌似不行,这里就给出刚刚码好的代码 ...

  5. Javascript实例技巧精选(8)—计算当月剩余天数

    >>点击这里下载完整html源码<< 截图如下: 利用Javascript在网页上计算当前月份的剩余天数,相应代码如下: <script language="J ...

  6. 依赖注入和IOC

    http://www.bbsmvc.com/archiver/csharp/thread-831-1.html 本来想使用一下Ninject的,然后搜索了很久,都没找到比较详细的关于Ninject的使 ...

  7. [置顶] Hibernate从入门到精通(十)多对多单向关联映射

    上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...

  8. Binder机制,从Java到C (7. Native Service)

    1.什么是NativeService Native Service,是通过C或C++代码写出來,提供给Java进行远程调用的RemoteService.向Android开机就启动的surfacefli ...

  9. Python Learing(二):Basic Image Processing 1

    <写在前面> Basic image processing 1: 0.(以简单的曲线图为例)对于生成的图自定义外观,使用子图,多个数据集,标题,标签,交互式标注,图例: 1.生成散点图,直 ...

  10. Android开发效率的小技巧

    提高eclipse使用效率(二) 提高Android开发效率的小技巧   XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Wi ...