using System.Drawing.Imaging; using System.Text; namespace FATrace.App.UniCodeToZPL { /// /// Unicode字符转化成对应的ZPL指令 /// public class UnicodeToZPLHelper { /// /// 生成图片回调显示 /// public static event Action OnViewImageCallBack; /// /// 未压缩算法处理 /// Unicode字符转化成对应的ZPL指令 /// /// 文本内容 /// 生成的图片名称,名称需要唯一 /// 字体 /// 文字方向 /// /// 返回转换完未压缩的ZPL指令 /// public static string UnCompressZPL(string content, string name, Font font, TextDirection textDirection) { Bitmap img = TextToBitmap(content, font, textDirection); if (textDirection != TextDirection.Degree0) { img = Rotate(img, (int)textDirection); } if (OnViewImageCallBack != null) { OnViewImageCallBack(img); } string imgCode = ConvertToZebraGRF(img); var totalBytes = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString(); //图形中的总字节数 var lineBytes = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString(); //每行的字节数 return string.Format("~DG{0}.GRF,{1},{2},{3}^XG{0}", name, totalBytes, lineBytes, imgCode); } /// /// 未压缩算法处理 /// Unicode字符转化成对应的ZPL指令 /// /// 文本内容 /// 生成的图片名称,名称需要唯一 /// 文字方向 /// /// 返回转换完未压缩的ZPL指令 /// public static string UnCompressZPL(Bitmap img, string name, TextDirection textDirection) { if (textDirection != TextDirection.Degree0) { img = Rotate(img, (int)textDirection); } if (OnViewImageCallBack != null) { OnViewImageCallBack(img); } string imgCode = ConvertToZebraGRF(img); var totalBytes = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString(); //图形中的总字节数 var lineBytes = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString(); //每行的字节数 return string.Format("~DG{0}.GRF,{1},{2},{3}^XG{0}", name, totalBytes, lineBytes, imgCode); } /// /// 压缩算法处理 /// Unicode字符转化成对应的ZPL指令 /// /// 文本内容 /// 生成的图片名称,名称需要唯一 /// 字体 /// 文字方向 /// /// 返回转换完压缩的ZPL指令 /// public static string CompressZPL(string content, string name, Font font, TextDirection textDirection) { Bitmap img = TextToBitmap(content, font, textDirection); if (textDirection != TextDirection.Degree0) { img = Rotate(img, (int)textDirection); } if (OnViewImageCallBack != null) { OnViewImageCallBack(img); } string imgCode = ConvertToZebraGRF(img); imgCode = ZebraCompress.ZebraCompressCode(imgCode); var totalBytes = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString(); //图形中的总字节数 var lineBytes = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString(); //每行的字节数 return string.Format("~DG{0}.GRF,{1},{2},{3}^XG{0}", name, totalBytes, lineBytes, imgCode); } /// /// 把文字转换才Bitmap /// /// /// /// private static Bitmap TextToBitmap(string text, Font font, TextDirection textDirection) { StringFormat format = new StringFormat(StringFormatFlags.NoClip); Bitmap bitmap = new Bitmap(1, 1); Graphics graphics = Graphics.FromImage(bitmap); SizeF sizef = graphics.MeasureString(text, font, PointF.Empty, format); int width = (int)(sizef.Width + 1); int height = (int)(sizef.Height + 1); Rectangle rect = new Rectangle(0, 0, width, height); bitmap.Dispose(); bitmap = new Bitmap(width, height); graphics = Graphics.FromImage(bitmap); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;//使用ClearType字体功能 graphics.FillRectangle(new SolidBrush(Color.White), rect); graphics.DrawString(text, font, Brushes.Black, rect, format); return bitmap; } /// /// 以逆时针为方向对图像进行旋转 /// /// 位图流 /// 旋转角度[0,360](前台给的) /// private static Bitmap Rotate(Bitmap b, int angle) { angle = angle % 360; //弧度转换 double radian = angle * Math.PI / 180.0; double cos = Math.Cos(radian); double sin = Math.Sin(radian); //原图的宽和高 int w = b.Width; int h = b.Height; int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin))); int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos))); //目标位图 Bitmap dsImage = new Bitmap(W, H); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //计算偏移量 Point Offset = new Point((W - w) / 2, (H - h) / 2); //构造图像显示区域:让图像的中心与窗口的中心点一致 Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h); Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2); g.TranslateTransform(center.X, center.Y); g.RotateTransform(360 - angle); //恢复图像在水平和垂直方向的平移 g.TranslateTransform(-center.X, -center.Y); g.DrawImage(b, rect); //重至绘图的所有变换 g.ResetTransform(); g.Save(); g.Dispose(); return dsImage; } /// /// 将BitMap图片转化成对应的灰度图片 /// 取灰度分界中间值,作为黑白像素点的判断,187数值比较合适 /// /// /// private static unsafe string ConvertToZebraGRF(Bitmap bitMap) { System.Text.StringBuilder builder = new StringBuilder(); BitmapData data = bitMap.LockBits(new Rectangle(0, 0, bitMap.Width, bitMap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); var bp = (byte*)data.Scan0.ToPointer(); for (int i = 0; i < data.Height; i++) { int count = 0; int tempData = 0; for (int j = 0; j < data.Width; j++) { float gray = 0.11F * bp[i * data.Stride + j * 3] + 0.59F * bp[i * data.Stride + j * 3 + 1] + 0.3F * bp[i * data.Stride + j * 3 + 2]; tempData = tempData * 2; if (gray < 187) { tempData++; } count++; if (j == (data.Width - 1)) { if (count < 8) { tempData = tempData * (2 ^ (8 - (int)count)); string temp = tempData.ToString("X").PadLeft(2, '0'); builder.Append(temp); tempData = 0; count = 0; } } if (count >= 8) { string temp = tempData.ToString("X").PadLeft(2, '0'); builder.Append(temp); tempData = 0; count = 0; } } } bitMap.UnlockBits(data); return builder.ToString(); } } }