在c#.net中生成条形码

时间:2014-03-24 06:13:52

标签: asp.net c#-4.0 barcode

我正在开发一个项目,我希望根据用户ID生成条形码。我需要一个可以生成条形码并且也是加密的代码。我找到了一些代码,但它们似乎没什么帮助。提前感谢您的帮助。我试过这段代码,但它没有提供条形码图像。

`private void Page_Load(object sender, System.EventArgs e)
{
     // Get the Requested code to be created.
     string Code = Request["code"].ToString();

     // Multiply the lenght of the code by 40 (just to have enough width)
     int w = Code.Length * 40;

    // Create a bitmap object of the width that we calculated and height of 100
     Bitmap oBitmap = new Bitmap(w,100);

    // then create a Graphic object for the bitmap we just created.
     Graphics oGraphics = Graphics.FromImage(oBitmap);

    // Now create a Font object for the Barcode Font
    // (in this case the IDAutomationHC39M) of 18 point size
    Font oFont = new Font("IDAutomationHC39M", 18);

    // Let's create the Point and Brushes for the barcode
    PointF oPoint = new PointF(2f, 2f);
     SolidBrush oBrushWrite = new SolidBrush(Color.Black);
     SolidBrush oBrush = new SolidBrush(Color.White);

    // Now lets create the actual barcode image
    // with a rectangle filled with white color
    oGraphics.FillRectangle(oBrush, 0, 0, w, 100);

    // We have to put prefix and sufix of an asterisk (*),
    // in order to be a valid barcode
    oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);

    // Then we send the Graphics with the actual barcode
    Response.ContentType = "image/jpeg" ;
     oBitmap.Save (Response.OutputStream, ImageFormat.Jpeg);
}`

2 个答案:

答案 0 :(得分:1)

查看this link这是一个简单的代码39条形码显示,支持页眉和页脚,打印,保存,并且可以很好地自定义。对于加密部分,您必须先将邮件加密,然后再将其转换为条形码。

从链接。

  

使用代码代码使用非常简单,只需控制即可   在表单上,​​您已准备好开始通过它自定义它   属性窗口或通过您的代码。除了   物业,还有两个感兴趣的公共职能:公共   void Print()此函数将显示打印对话框然后打印   控件的内容到选定的打印机。公共无效   SaveImage(string filename)这个函数将保存的内容   控制到由filename指定的位图图像。

http://www.codeproject.com/Articles/10344/Barcode-NET-Control

答案 1 :(得分:0)

请尝试使用以下代码,您必须在代码中添加" FREE3OF9.TTF" 字体文件

Default.aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ////GenerateBarCode.GenerateBarCodes("dfdfdf", Label1);
            GenerateBarCode();
        }
    }
    private void GenerateBarCode()
    {

        WSBarcodeGenerator.BarCodeGenerator barCodeGen = new WSBarcodeGenerator.BarCodeGenerator();

        int barSize = 30;

        System.Byte[] imgBarcode =Code39("123456", barSize, true, "Centralbiz...");
        MemoryStream memStream = new MemoryStream(imgBarcode);
        Bitmap bitmap = new Bitmap(memStream);
        bitmap.Save(memStream, ImageFormat.Png);
        var base64Data = Convert.ToBase64String(memStream.ToArray());
        imgBar.Attributes.Add("src", "data:image/png;base64," + base64Data);
        //Response.Write(bitmap);
        //var base64Data = Convert.ToBase64String(memStream.ToArray());
        //imgBar.Attributes.Add("src", "png");
    }
    public byte[] Code39(string code, int barSize, bool showCodeString, string title)
    {

        Code39 c39 = new Code39();

        // Create stream....
        MemoryStream ms = new MemoryStream();
        c39.FontFamilyName = "Free 3 of 9";
        c39.FontFileName = Server.MapPath("FREE3OF9.TTF");
        c39.FontSize = barSize;
        c39.ShowCodeString = showCodeString;
        if (title + "" != "")
            c39.Title = title;
        Bitmap objBitmap = c39.GenerateBarcode(code);
        objBitmap.Save(ms, ImageFormat.Png);

        //return bytes....
        return ms.GetBuffer();
    }

}
public class Code39
{
    private const int _itemSepHeight = 3;

    SizeF _titleSize = SizeF.Empty;
    SizeF _barCodeSize = SizeF.Empty;
    SizeF _codeStringSize = SizeF.Empty;

    #region Barcode Title

    private string _titleString = null;
    private Font _titleFont = null;

    public string Title
    {
        get { return _titleString; }
        set { _titleString = value; }
    }

    public Font TitleFont
    {
        get { return _titleFont; }
        set { _titleFont = value; }
    }
    #endregion

    #region Barcode code string

    private bool _showCodeString = false;
    private Font _codeStringFont = null;

    public bool ShowCodeString
    {
        get { return _showCodeString; }
        set { _showCodeString = value; }
    }

    public Font CodeStringFont
    {
        get { return _codeStringFont; }
        set { _codeStringFont = value; }
    }
    #endregion

    #region Barcode Font

    private Font _c39Font = null;
    private float _c39FontSize = 12;
    private string _c39FontFileName = null;
    private string _c39FontFamilyName = null;

    public string FontFileName
    {
        get { return _c39FontFileName; }
        set { _c39FontFileName = value; }
    }

    public string FontFamilyName
    {
        get { return _c39FontFamilyName; }
        set { _c39FontFamilyName = value; }
    }

    public float FontSize
    {
        get { return _c39FontSize; }
        set { _c39FontSize = value; }
    }

    private Font Code39Font
    {
        get
        {
            if (_c39Font == null)
            {
                PrivateFontCollection pfc = new PrivateFontCollection();
                pfc.AddFontFile(_c39FontFileName);
                FontFamily family = new FontFamily(_c39FontFamilyName, pfc);
                _c39Font = new Font(family, _c39FontSize);
            }
            return _c39Font;
        }
    }

    #endregion

    public Code39()
    {
        _titleFont = new Font("Arial", 10);
        _codeStringFont = new Font("Arial", 10);
    }

    #region Barcode Generation

    public Bitmap GenerateBarcode(string barCode)
    {

        int bcodeWidth = 0;
        int bcodeHeight = 0;

        // Get the image container...
        Bitmap bcodeBitmap = CreateImageContainer(barCode, ref bcodeWidth, ref bcodeHeight);
        Graphics objGraphics = Graphics.FromImage(bcodeBitmap);

        // Fill the background          
        objGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bcodeWidth, bcodeHeight));

        int vpos = 0;

        // Draw the title string
        if (_titleString != null)
        {
            objGraphics.DrawString(_titleString, _titleFont, new SolidBrush(Color.Black), XCentered((int)_titleSize.Width, bcodeWidth), vpos);
            vpos += (((int)_titleSize.Height) + _itemSepHeight);
        }
        // Draw the barcode
        objGraphics.DrawString(barCode, Code39Font, new SolidBrush(Color.Black), XCentered((int)_barCodeSize.Width, bcodeWidth), vpos);

        // Draw the barcode string
        if (_showCodeString)
        {
            vpos += (((int)_barCodeSize.Height));
            objGraphics.DrawString(barCode, _codeStringFont, new SolidBrush(Color.Black), XCentered((int)_codeStringSize.Width, bcodeWidth), vpos);
        }

        // return the image...                                  
        return bcodeBitmap;
    }

    private Bitmap CreateImageContainer(string barCode, ref int bcodeWidth, ref int bcodeHeight)
    {

        Graphics objGraphics;

        // Create a temporary bitmap...
        Bitmap tmpBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
        objGraphics = Graphics.FromImage(tmpBitmap);

        // calculate size of the barcode items...
        if (_titleString != null)
        {
            _titleSize = objGraphics.MeasureString(_titleString, _titleFont);
            bcodeWidth = (int)_titleSize.Width;
            bcodeHeight = (int)_titleSize.Height + _itemSepHeight;
        }

        _barCodeSize = objGraphics.MeasureString(barCode, Code39Font);
        bcodeWidth = Max(bcodeWidth, (int)_barCodeSize.Width);
        bcodeHeight += (int)_barCodeSize.Height;

        if (_showCodeString)
        {
            _codeStringSize = objGraphics.MeasureString(barCode, _codeStringFont);
            bcodeWidth = Max(bcodeWidth, (int)_codeStringSize.Width);
            bcodeHeight += (_itemSepHeight + (int)_codeStringSize.Height);
        }

        // dispose temporary objects...
        objGraphics.Dispose();
        tmpBitmap.Dispose();

        return (new Bitmap(bcodeWidth, bcodeHeight, PixelFormat.Format32bppArgb));
    }

    #endregion


    #region Auxiliary Methods

    private int Max(int v1, int v2)
    {
        return (v1 > v2 ? v1 : v2);
    }

    private int XCentered(int localWidth, int globalWidth)
    {
        return ((globalWidth - localWidth) / 2);
    }

    #endregion

}

Default.aspx代码

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>

    <asp:Image ID="imgBar"   runat="server" />
</asp:Content>