809
社区成员
发帖
与我相关
我的任务
分享
private static bool StringHasChinese(string strTmp)//这个函数式判断一字符转是否包含汉子
{
Regex r = new Regex(@"[\u4e00-\u9fa5]+");
Match mc = r.Match(strTmp);
if (mc.Length != 0)
{
return true;
}
else
{
return false;
}
}
public static void PrintLabel(string defectname)
{
int pad = 10;
Bitmap bitmap = new Bitmap(355, 45);
Graphics g = Graphics.FromImage(bitmap);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, bitmap.Width, bitmap.Height);
g.DrawString(defectname, new Font("黑体", 28, FontStyle.Bold), new SolidBrush(Color.Black), new PointF(10, 5));
string txt = "^XA^LH" + (pad).ToString() + "," + (pad).ToString() + "^FS";
txt += Convert(bitmap, defectname, 240, 50);
txt += "^XZ";
//Write(txt);发送到LPT并口的
}
public static string Convert(Bitmap bm,string txt, int left, int top)
{
try
{
int b = 0;
long n = 0;
long clr;
StringBuilder sb = new StringBuilder();
sb.Append("~DGR:ZONE.GRF,");
int w = (bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)) * bm.Size.Height;
int h = bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1);
//string s;
sb.Append(w.ToString().PadLeft(5, '0') + "," + h.ToString().PadLeft(3, '0') + ",\n");
using (Bitmap bmp = new Bitmap(bm.Size.Width, bm.Size.Height))
{
for (int y = 0; y < bm.Size.Height; y++)
{
for (int x = 0; x < bm.Size.Width; x++)
{
b = b * 2;
clr = bm.GetPixel(x, y).ToArgb();
string s = clr.ToString("X");
if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0)
{
bmp.SetPixel(x, y, bm.GetPixel(x, y));
b++;
}
n++;
if (x == (bm.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("^FO" + left.ToString() + "," + top.ToString() + "^XGR:ZONE.GRF^FS");
}
return sb.ToString();
}
catch
{
return txt;
}
}