Asp.net 在c#net中生成条形码

Asp.net 在c#net中生成条形码,asp.net,c#-4.0,barcode,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"].ToStri

我在一个项目上工作,我想生成一个基于用户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);
}`
检查它是一个简单的代码39条码显示,它支持页眉和页脚,打印,保存,并且是非常好的自定义。对于加密部分,您必须在将邮件转换为条形码之前对其进行加密

从链接

使用该代码的代码使用非常简单,只需plop控件 然后,您就可以开始通过 属性窗口或通过您的代码。除了 属性中,还有两个感兴趣的公共功能:公共 void Print()此函数将显示打印对话框,然后打印 将控件的内容添加到所选打印机。公共空间 SaveImage(字符串文件名)此函数将保存 控件设置为由文件名指定的位图图像


请尝试以下代码,您必须在代码中添加“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

}
<%@ 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>
Default.aspx代码

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

}
<%@ 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>

.

你也可以找到。


到目前为止您尝试了什么?