【发布时间】:2014-03-17 12:27:50
【问题描述】:
我正在使用 IDAutomationHC39M 字体生成条形码。
如果我需要在客户端系统中工作,我必须安装该字体。
如何绕过这个解决方案?
【问题讨论】:
我正在使用 IDAutomationHC39M 字体生成条形码。
如果我需要在客户端系统中工作,我必须安装该字体。
如何绕过这个解决方案?
【问题讨论】:
看看这篇 CodeProject 文章。他在不使用字体的情况下生成 Code 39 条码。
http://www.codeproject.com/Articles/10344/Barcode-NET-Control
还有这个SO问题:
How to generate Code39 barcodes in vb.net
在 Google 上快速搜索“Code 39 条码 .net”也会为您提供一些免费和商业的条码生成库和控件。
【讨论】:
您可以将字体作为资源添加到项目中,然后检索它:
var value = Resources.MyFont; // it's stored as byte[]
var fonts = new PrivateFontCollection();
var memory = IntPtr.Zero;
try
{
memory = Marshal.AllocCoTaskMem(value.Length);
Marshal.Copy(value, 0, memory, value.Length);
fonts.AddMemoryFont(memory, value.Length);
}
finally
{
Marshal.FreeCoTaskMem(memory);
}
var font = new Font(fonts.Families[0], 12F); // choose the size
【讨论】:
这是我使用的扩展方法:
public static Image GetBarCode(this string data, int fontSizeEm)
{
data = "*" + data + "*";
Image img = null;
Graphics drawing = null;
try
{
using (var pfc = new PrivateFontCollection())
{
pfc.AddFontFile(@"{{PATH TO YOUR FONT}}");
using (var myfont = new Font(pfc.Families[0], fontSizeEm))
{
img = new Bitmap(1, 1);
drawing = Graphics.FromImage(img);
var textSize = drawing.MeasureString(data, myfont);
img.Dispose();
drawing.Dispose();
img = new Bitmap((int) textSize.Width, (int) textSize.Height);
drawing = Graphics.FromImage(img);
drawing.DrawString(data, myfont, new SolidBrush(Color.Black), 0, 0);
}
drawing.Save();
}
return img;
}
catch
{
//Handle exception
return null;
}
finally
{
if(drawing!= null)
drawing.Dispose();
}
}
【讨论】: