ASP.net(c#)生成條形碼 code39條碼生成方法
1.先下載一種免費(fèi)的 code39條碼字體
2.建個(gè)類 為 code39 并寫(xiě)入以下代碼
public sealed class Code39
{
#region private variables
/// <summary>
/// The Space Between each of Title, BarCode, BarCodeString
/// </summary>
private const int SPACE_HEIGHT = 3;
SizeF _sizeLabel = SizeF.Empty;
SizeF _sizeBarCodeValue = SizeF.Empty;
SizeF _sizeBarCodeString = SizeF.Empty;
#endregion
#region Label
private string _label = null;
private Font _labelFont = null;
/// <summary>
/// BarCode Title (條碼標(biāo)簽)
/// </summary>
public string Label
{
set { _label = value; }
}
/// <summary>
/// BarCode Title Font (條碼標(biāo)簽使用的字體)
/// </summary>
public Font LabelFont
{
get
{
if (_labelFont == null)
return new Font("Arial", 10);
return _labelFont;
}
set { _labelFont = value; }
}
#endregion
private string _additionalInfo = null;
private Font _addtionalInfoFont = null;
/// <summary>
/// Additional Info Font (附加信息字體)
/// </summary>
public Font AdditionalInfoFont
{
set { _addtionalInfoFont = value; }
get
{
if (_addtionalInfoFont == null) return new Font("Arial", 10);
return _addtionalInfoFont;
}
}
/// <summary>
/// Additional Info Content, if "ShowBarCodeValue" is true, the info is unvisible
/// 附加信息,如果設(shè)置ShowBarCodeValue為true,則此項(xiàng)不顯示
/// </summary>
public string AdditionalInfo
{
set { _additionalInfo = value; }
}
#region BarCode Value and Font
private string _barCodeValue = null;
/// <summary>
/// BarCode Value (條碼值)
/// </summary>
public string BarCodeValue
{
get
{
if (string.IsNullOrEmpty(_barCodeValue))
throw new NullReferenceException("The BarCodeValue has not been set yet!");
return _barCodeValue;
}
set { _barCodeValue = value.StartsWith("*") && value.EndsWith("*") ? value : "*" + value + "*"; }
}
private bool _showBarCodeValue = false;
/// <summary>
/// whether to show the original string of barcode value bellow the barcode
/// 是否在條碼下方顯示條碼值,默認(rèn)為false
/// </summary>
public bool ShowBarCodeValue
{
set { _showBarCodeValue = value; }
}
private Font _barCodeValueFont = null;
/// <summary>
/// the font of the codestring to show
/// 條碼下方顯示的條碼值的字體
/// </summary>
public Font BarCodeValueFont
{
get
{
if (_barCodeValueFont == null)
return new Font("Arial", 10);
return _barCodeValueFont;
}
set { _barCodeValueFont = value; }
}
private int _barCodeFontSize = 50;
/// <summary>
/// the font size of the barcode value to draw
/// 條碼繪制的大小,默認(rèn)50
/// </summary>
public int BarCodeFontSzie
{
set { _barCodeFontSize = value; }
}
#endregion
#region generate the barcode image
private Bitmap BlankBackImage
{
get
{
int barCodeWidth = 0, barCodeHeight = 0;
using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bmp))
{
if (!string.IsNullOrEmpty(_label))
{
_sizeLabel = g.MeasureString(_label, LabelFont);
barCodeWidth = (int)_sizeLabel.Width;
barCodeHeight = (int)_sizeLabel.Height + SPACE_HEIGHT;
}
_sizeBarCodeValue = g.MeasureString(BarCodeValue, new Font("Free 3 of 9 Extended", _barCodeFontSize));
barCodeWidth = Math.Max(barCodeWidth, (int)_sizeBarCodeValue.Width);
barCodeHeight += (int)_sizeBarCodeValue.Height;
if (_showBarCodeValue)
{
_sizeBarCodeString = g.MeasureString(_barCodeValue, BarCodeValueFont);
barCodeWidth = Math.Max(barCodeWidth, (int)_sizeBarCodeString.Width);
barCodeHeight += (int)_sizeBarCodeString.Height + SPACE_HEIGHT;
}
//else
//{
// if (!string.IsNullOrEmpty(_additionalInfo))
// {
// _sizeAdditionalInfo = g.MeasureString(_additionalInfo, AdditionalInfoFont);
// barCodeWidth = Math.Max(barCodeWidth, (int)_sizeAdditionalInfo.Width);
// barCodeHeight += (int)_sizeAdditionalInfo.Height + SPACE_HEIGHT;
// }
//}
}
}
return new Bitmap(barCodeWidth, barCodeHeight, PixelFormat.Format32bppArgb);
}
}
/// <summary>
/// Draw the barcode value to the blank back image and output it to the browser
/// 繪制WebForm形式的條碼
/// </summary>
/// <param name="ms">Recommand the "Response.OutputStream" 使用 Response.OutputStream</param>
/// <param name="imageFormat">The Image format to the Browser 輸出到瀏覽器到圖片格式,建議GIF</param>
public Bitmap CreateWebForm(Stream ms, ImageFormat imageFormat)
{
int barCodeWidth, barCodeHeight;
using (Bitmap bmp = this.BlankBackImage)
{
barCodeHeight = bmp.Height;
barCodeWidth = bmp.Width;
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
int vPos = 0;
////Draw Label String
if (!string.IsNullOrEmpty(_label))
{
g.DrawString(_label, LabelFont, new SolidBrush(Color.Black),
XCenter((int)_sizeLabel.Width, barCodeWidth), vPos);
vPos += (int)_sizeLabel.Height + SPACE_HEIGHT;
}
else { vPos = SPACE_HEIGHT; }
////Draw The Bar Value
g.DrawString(_barCodeValue, new Font("Free 3 of 9 Extended", _barCodeFontSize), new SolidBrush(Color.Black),
XCenter((int)_sizeBarCodeValue.Width, barCodeWidth), vPos);
////Draw the BarValue String
if (_showBarCodeValue)
{
g.DrawString(_barCodeValue, BarCodeValueFont, new SolidBrush(Color.Black),
XCenter((int)_sizeBarCodeString.Width, barCodeWidth),
vPos + (int)_sizeBarCodeValue.Height);
}
//else
//{
// if (!string.IsNullOrEmpty(_additionalInfo))
// {
// g.DrawString(_additionalInfo, AdditionalInfoFont, new SolidBrush(Color.Black),
// XCenter((int)_sizeAdditionalInfo.Width, barCodeWidth),
// vPos + (int)_sizeBarCodeValue.Height);
// }
//}
}
bmp.Save(ms, imageFormat);
return bmp;
}
}
/// <summary>
/// 生成winform格式的條碼
/// </summary>
/// <param name="imageFormat">圖片格式,建議GIF</param>
/// <returns>Stream類型</returns>
public Stream CreateWinForm(ImageFormat imageFormat)
{
int barCodeWidth, barCodeHeight;
using (Bitmap bmp = this.BlankBackImage)
{
barCodeHeight = bmp.Height;
barCodeWidth = bmp.Width;
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
int vPos = 0;
////Draw Label String
if (!string.IsNullOrEmpty(_label))
{
g.DrawString(_label, LabelFont, new SolidBrush(Color.Black),
XCenter((int)_sizeLabel.Width, barCodeWidth), vPos);
vPos += (int)_sizeLabel.Height + SPACE_HEIGHT;
}
else { vPos = SPACE_HEIGHT; }
////Draw The Bar Value
g.DrawString(_barCodeValue, new Font("Free 3 of 9 Extended", _barCodeFontSize), new SolidBrush(Color.Black),
XCenter((int)_sizeBarCodeValue.Width, barCodeWidth), vPos);
////Draw the BarValue String
if (_showBarCodeValue)
{
g.DrawString(_barCodeValue, BarCodeValueFont, new SolidBrush(Color.Black),
XCenter((int)_sizeBarCodeString.Width, barCodeWidth),
vPos + (int)_sizeBarCodeValue.Height);
}
//else
//{
// //if (!string.IsNullOrEmpty(_additionalInfo))
// //{
// // g.DrawString(_additionalInfo, AdditionalInfoFont, new SolidBrush(Color.Black),
// // //XCenter((int)_sizeAdditionalInfo.Width, barCodeWidth),
// // vPos + (int)_sizeBarCodeValue.Height);
// //}
//}
}
Stream ms = new MemoryStream();
bmp.Save(ms, imageFormat);
return ms;
}
}
#endregion
private static int XCenter(int subWidth, int globalWidth)
{
return (globalWidth - subWidth) / 2;
}
}
3.如果是web程序 請(qǐng)調(diào)用 CreateWebForm 如果是cs程序 則使用CreateWinForm
4.新建一aspx文件,寫(xiě)入以下代碼
protected void Page_Load(object sender, EventArgs e)
{
Code39 code39 = new Code39();
code39.BarCodeValue = "LDSO-001";
code39.BarCodeFontSzie = 60;
// code39.Label = "39碼,底部顯示碼值";
code39.ShowBarCodeValue = true;
Response.Write(code39.CreateWebForm(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif));
code39 = null;
}
- ASP.NET MVC3關(guān)于生成純靜態(tài)后如何不再走路由直接訪問(wèn)靜態(tài)頁(yè)面
- 使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
- ASP.NET動(dòng)態(tài)生成靜態(tài)頁(yè)面的實(shí)例代碼
- ASP.NET 生成靜態(tài)頁(yè)面 實(shí)現(xiàn)思路
- Asp.NET 生成靜態(tài)頁(yè)面并分頁(yè)的代碼
- Asp.Net生成靜態(tài)頁(yè)面的實(shí)現(xiàn)方法
- ASP.NET MVC生成靜態(tài)頁(yè)面的方法
- asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
- asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
- asp.net C#生成和解析二維碼的實(shí)例代碼
- Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁(yè)面而不是文件的問(wèn)題)
- ASP.NET編程簡(jiǎn)單實(shí)現(xiàn)生成靜態(tài)頁(yè)面的方法【附demo源碼下載】
相關(guān)文章
asp.net Server.MapPath方法注意事項(xiàng)
當(dāng)我發(fā)布之后,對(duì)存儲(chǔ)圖片的文件夾創(chuàng)建了虛擬目錄,并賦予該目錄寫(xiě)入的權(quán)限,但是,當(dāng)我上傳圖片的時(shí)候,總是失敗。以前沒(méi)遇到過(guò)這種情況,覺(jué)得很是怪異,所以想盡辦法去解決。2008-09-09關(guān)于HttpHandler與HttpModule的理解和應(yīng)用方法
本篇文章小編將為大家介紹,關(guān)于HttpHandler與HttpModule的理解和應(yīng)用方法,有需要的朋友可以參考一下2013-04-04GridView選擇記錄同時(shí)confirm用戶確認(rèn)刪除
confirm用戶確認(rèn)刪除是一個(gè)很實(shí)用的功能,比如可以防止用戶誤操作刪除等等,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助,就當(dāng)鞏固知識(shí)了2013-01-01詳解可跨域的單點(diǎn)登錄(SSO)實(shí)現(xiàn)方案【附.net代碼】
本篇文章主要介紹了可跨域的單點(diǎn)登錄(SSO)實(shí)現(xiàn)方案,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11asp.net利用存儲(chǔ)過(guò)程實(shí)現(xiàn)模糊查詢示例分享
這篇文章主要介紹了asp.net利用存儲(chǔ)過(guò)程實(shí)現(xiàn)模糊查詢的示例,大家參考使用吧2014-01-01ASP.NET Session的七點(diǎn)認(rèn)識(shí)小結(jié)
ASP.NET Session的使用當(dāng)中我們會(huì)遇到很多的問(wèn)題,那么這里我們來(lái)談下經(jīng)常出現(xiàn)的一些常用ASP.NET Session的理解2011-07-07.net mvc超過(guò)了最大請(qǐng)求長(zhǎng)度的解決方法
這篇文章主要為大家詳細(xì)介紹了.net mvc超過(guò)了最大請(qǐng)求長(zhǎng)度的解決方法,限制文件上傳大小,感興趣的小伙伴們可以參考一下2016-07-07.NET中的MassTransit分布式應(yīng)用框架詳解
MassTransit是一款優(yōu)秀的分布式應(yīng)用框架,可作為分布式應(yīng)用的消息總線,也可以用作單體應(yīng)用的事件總線,這篇文章主要介紹了.NET中的MassTransit分布式應(yīng)用框架,需要的朋友可以參考下2022-10-10the sourcesafe database has been locked by the administrator
今天早上打開(kāi)soucesafe的時(shí)候出現(xiàn)提示:“the sourcesafe database has been locked by the administrator"。仔細(xì)想想, 可能是前天晚上用"f:\analyze.exe" -I- -DB -F -V3 -D "f:\vssData\data" 命今分析的時(shí)候鎖定了database2009-04-04