using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RGD.Common
{
public class BarcodeUtils
{
//斑马打印机打印文本1.0,baseX,baseY文本起始屏幕位置 txtH, txtW文本高度和宽度 txtContent 文本内容
public static string PrintTextString(string baseX, string baseY, string txtH, string txtW, string txtContent)
{
return "^FO" + baseX + "," + baseY + "^ADN," + txtH + "," + txtW + "^CI0^FD" + txtContent + "^FS\r\n";
}
//斑马打印机打印条形码1.0,baseX,baseY条形码起始屏幕位置 linewidth条形码宽度 ratio打印比率 barcode 条形码内容
public static string PrintBarString(string baseX, string baseY, string linewidth, string ratio, string height, string barcode)
{
return "^FT" + baseX + "," + baseY + "^BY" + linewidth + "," + ratio + "," + height + "^BCN," + ",N,N^FD" + barcode + "^FS\r\n";
}
//斑马打印机打印文本2.0
public string PrintString(string printStr)
{
StringBuilder builder = new StringBuilder();
builder.Append("^XA\r\n");
builder.Append(printStr);
builder.Append("^XZ");
return builder.ToString();
}
//斑马打印机打印文本
public bool LabelPrint(string printerName, string printStr)
{
return RawPrinterHelper.SendStringToPrinter(printerName, printStr);
}
//斑马打印机创建条形码上方图片
public static Bitmap CreateImage(string data, Font f)
{
if (string.IsNullOrEmpty(data))
return null;
var txt = new TextBox();
txt.Text = data;
txt.Font = f;
//txt.PreferredSize.Height只能取到一行的高度(连边距)
//所以需要乘以行数, 但是必须先减掉边距, 乘了以后,再把边距加上.
//5是目测的边距
var image = new Bitmap(txt.PreferredSize.Width, (txt.PreferredSize.Height - 5) * txt.Lines.Length);
// var image = new Bitmap(300, (txt.PreferredSize.Height - 5) * txt.Lines.Length);
//var image = new Bitmap(txt.PreferredSize.Width + 20, 13);
var g = Graphics.FromImage(image);
var b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Black, 1.2f, true);
g.Clear(System.Drawing.Color.White);
g.DrawString(data, f, b, 1, 1);
return image;
}
//斑马打印机创建条形码下方图片(等比例拉伸图片)
public static Bitmap CreateImage2(string data, Font f)
{
if (string.IsNullOrEmpty(data))
return null;
var txt = new TextBox();
txt.Text = data;
txt.Font = f;
//txt.PreferredSize.Height只能取到一行的高度(连边距)
//所以需要乘以行数, 但是必须先减掉边距, 乘了以后,再把边距加上.
//5是目测的边距
var image = new Bitmap(txt.PreferredSize.Width, (txt.PreferredSize.Height - 5) * txt.Lines.Length);
// var image = new Bitmap(300, (txt.PreferredSize.Height - 5) * txt.Lines.Length);
//var image = new Bitmap(txt.PreferredSize.Width + 20, 13);
var g = Graphics.FromImage(image);
var b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Black, 1.2f, true);
g.Clear(System.Drawing.Color.White);
g.DrawString(data, f, b, 1, 1);
var image2 = new Bitmap(300, image.Height);
var g2 = Graphics.FromImage(image2);
g2.DrawImage(image, new Rectangle(0, 0, 300, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
return image2;
}
//将bmp转为ZLP码
public static string CreateBmpZLP(Bitmap img, string imgCode, string imgName)
{
var t = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString();
var w = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString();
string zpl = string.Format("~DG" + imgName + ".GRF,{0},{1},{2}", t, w, imgCode);
return zpl;
}
//将图片转为16进制码
public static string ConvertImageToCode(Bitmap img)
{
var sb = new StringBuilder();
long clr = 0, n = 0;
int b = 0;
for (int i = 0; i < img.Size.Height; i++)
{
for (int j = 0; j < img.Size.Width; j++)
{
b = b * 2;
clr = img.GetPixel(j, i).ToArgb();
string s = clr.ToString("X");
if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0)
{
b++;
}
n++;
if (j == (img.Size.Width - 1))
{
if (n < 8)
{
b = b * (2 ^ (8 - (int)n));
sb.Append(b.ToString("X").PadLeft(2, '0'));
b = 0;
n = 0;
}
}
if (n >= 8)
{
sb.Append(b.ToString("X").PadLeft(2, '0'));
b = 0;
n = 0;
}
}
sb.Append(System.Environment.NewLine);
}
return sb.ToString();
}
#region
///
/// 验证是否是指定位数,并以指定字符串开头
///
/// 输入字符
/// 返回一个bool类型的值
public static bool validateBarcode(string M_str_src, string M_str_prefix)
{
return Regex.IsMatch(M_str_src, @"(^" + M_str_prefix + "[0-9]*$)");
}
#endregion
#region
///
/// 验证是否是指定位数,并以指定字符串开头
///
/// 输入字符
/// 返回一个bool类型的值
public static bool validateBoxCode(string M_str_src, string[] M_str_prefix)
{
bool result = false;
foreach (string prefix in M_str_prefix)
{
result = Regex.IsMatch(M_str_src, @"(^" + prefix + "[0-9]*$)");
if (result)
{
break;
}
}
return result;
}
#endregion
}
}