本文介绍了如何在ASP.NET中打印条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的网页上打印条形码。
I am trying to print a barcode in my web page.
string barcode = Label23.Text;
Bitmap bitmap = new Bitmap(barcode.Length * 40,150);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Font ofont = new System.Drawing.Font("IDAutomationHC39M", 20);
PointF point = new PointF(2f, 2f);
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush white = new SolidBrush(Color.White);
graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
graphics.DrawString("*"+barcode+"*",ofont,black,point);
}
using (MemoryStream ms = new MemoryStream())
{
string mypath = Request.PhysicalApplicationPath + "Images\\";
bitmap.Save(mypath+ "barcode.png",ImageFormat.Png);
Image2.ImageUrl = "~/Images/barcode.png";
}
我的尝试:
而不是获取条形码我得到的是我输入的文本。
What I have tried:
instead of getting the barcode i am getting the text that i enter.
推荐答案
public void CreateBarcode(string data) {
var imgPath = string.Format("{0}{1}.png", path, data);
Bitmap barcode = new Bitmap(1, 1);
Font threeOfNine = new Font("IDAutomationHC39M", 60,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point);
Graphics graphics = Graphics.FromImage(barcode);
SizeF dataSize = graphics.MeasureString(data, threeOfNine);
barcode = new Bitmap(barcode, dataSize.ToSize());
graphics = Graphics.FromImage(barcode);
graphics.Clear(Color.White);
graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
graphics.DrawString(data, threeOfNine, new SolidBrush(Color.Black), 0, 0);
graphics.Flush();
threeOfNine.Dispose();
graphics.Dispose();
barcode.Save(imgPath);
}
这篇关于如何在ASP.NET中打印条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!