using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using ZXing; using ZXing.Common; using ZXing.QrCode; namespace JJMediSys { public class RecpCreate { SolidBrush sbrush = new SolidBrush(System.Drawing.Color.Black); StringFormat strFormat = new StringFormat(); //static int RecpW = 448; //static int RecpH = 240; static int RecpW = 672; static int RecpH = 354; int PerLineLen = RecpW / 8; public Image CreateImage(string Indate, ref Byte[] ImgaeData) { TubeLabelTool.logger.Info("RecpInfo: " + Indate); if (SystemSet.RecpImageSize == 29736) { RecpW = 672; RecpH = 354; } else { RecpW = 448; RecpH = 240; } PerLineLen = RecpW / 8; strFormat.Alignment = StringAlignment.Near; //左对齐 strFormat.LineAlignment = StringAlignment.Near; Image img = new Bitmap(RecpW, RecpH); Graphics g = Graphics.FromImage(img); g.DrawImage(img, 1, 1, RecpW, RecpH); g.Clear(Color.White); string[] Arrstr = Indate.Split('@'); foreach (string Temp in Arrstr) { Draw(g, Temp); } Bitmap bmp = new Bitmap(RecpW, RecpH); Graphics gg = Graphics.FromImage(bmp); gg.DrawImage(img, 0, 0, new Rectangle(0, 0, RecpW, RecpH), GraphicsUnit.Pixel); // Byte[] PrintDate = new byte[56 * 240]; Bitmap mybitmap = new Bitmap(img); Color srcColor; int wide = mybitmap.Width; int height = mybitmap.Height; int Index = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < wide; x++) { //获取像素的RGB颜色值 srcColor = mybitmap.GetPixel(x, y); if (srcColor.R != 255) { Index = y * PerLineLen + x / 8; ImgaeData[Index] |= (byte)(0x80 >> (x % 8)); } } } string Hexstr = ToHexStrFromByte(ImgaeData); //img.Save("D:\\1.bmp"); g.Dispose(); gg.Dispose(); return img; } public Image CreateBLEImage(string Indate, ref Byte[] ImgaeDataBlack, ref Byte[] ImgaeDataRed) { RecpW = 250; RecpH = 128; PerLineLen = RecpW / 8; strFormat.Alignment = StringAlignment.Near; //左对齐 strFormat.LineAlignment = StringAlignment.Near; Image img = new Bitmap(RecpW, RecpH); Graphics g = Graphics.FromImage(img); /****************设置图片质量 防止有噪点*****************/ g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(img, 1, 1, RecpW, RecpH); g.Clear(Color.White); string[] Arrstr = Indate.Split('@'); foreach (string Temp in Arrstr) { Draw(g, Temp); } if (Indate.Length == 0) { Pen pen = new Pen(Color.Black, 2); g.DrawLine(pen, 1, 0, 1, 20); g.DrawLine(pen, 5, 0, 5, 20); g.DrawLine(pen, 10, 0, 10, 20); pen.Dispose(); } Bitmap bmp = new Bitmap(RecpW, RecpH); Graphics gg = Graphics.FromImage(bmp); gg.DrawImage(img, 0, 0, new Rectangle(0, 0, RecpW, RecpH), GraphicsUnit.Pixel); Bitmap mybitmap = new Bitmap(img); Color srcColor; int wide = mybitmap.Width; int height = mybitmap.Height; int Index = 0; int posion = 0; for (int x = 0; x < wide; x++) { for (int y = 0; y < height; y++) { //获取像素的RGB颜色值 srcColor = mybitmap.GetPixel(x, y); if (srcColor.R == 0) //黑色 { Index = posion / 8; ImgaeDataBlack[Index] |= (byte)(0x80 >> (posion % 8)); } else if (srcColor.R == 255 && srcColor.G != 255) //红色 { Index = posion / 8; ImgaeDataRed[Index] |= (byte)(0x80 >> (posion % 8)); } posion++; } } g.Dispose(); gg.Dispose(); return img; } public Byte[] CreateImageDate(string Indate) { strFormat.Alignment = StringAlignment.Near; //左对齐 strFormat.LineAlignment = StringAlignment.Near; Image img = new Bitmap(RecpW, RecpH); Graphics g = Graphics.FromImage(img); g.DrawImage(img, 1, 1, RecpW, RecpH); g.Clear(Color.White); string[] Arrstr = Indate.Split('@'); foreach (string Temp in Arrstr) { Draw(g, Temp); } Bitmap bmp = new Bitmap(RecpW, RecpH); Graphics gg = Graphics.FromImage(bmp); gg.DrawImage(img, 0, 0, new Rectangle(0, 0, RecpW, RecpH), GraphicsUnit.Pixel); Byte[] PrintDate = new byte[PerLineLen * RecpH]; Bitmap mybitmap = new Bitmap(img); Color srcColor; int wide = mybitmap.Width; int height = mybitmap.Height; int Index = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < wide; x++) { //获取像素的RGB颜色值 srcColor = mybitmap.GetPixel(x, y); if (srcColor.R != 255) { Index = y * PerLineLen + x / 8; PrintDate[Index] |= (byte)(0x80 >> (x % 8)); } } } //string Hexstr = ToHexStrFromByte(PrintDate); g.Dispose(); gg.Dispose(); return PrintDate; } public void Draw(Graphics g, string InData) { if (InData == "") return; SolidBrush drawbrush = sbrush; string[] Arrstr = InData.Split('|'); if (Arrstr[0].Substring(0, 1) == "T") { if (Arrstr[0].Length != 16) return; Font font; int FontSize = int.Parse(Arrstr[0].Substring(2, 2)); int X = int.Parse(Arrstr[0].Substring(4, 3)); int Y = int.Parse(Arrstr[0].Substring(7, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(10, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(13, 3)); if (Arrstr[0].Substring(1, 1) == "B") { font = new Font("宋体", FontSize, FontStyle.Bold); } else if (Arrstr[0].Substring(1, 1) == "U") { font = new Font("宋体", FontSize, FontStyle.Underline); } else { font = new Font("宋体", FontSize); } string[] Temp = Arrstr[1].Split('^'); if (Temp.Length == 1) { StringFormat tempstrFormat = new StringFormat(); tempstrFormat.LineAlignment = strFormat.LineAlignment; tempstrFormat.Alignment = StringAlignment.Center; g.DrawString(Arrstr[1] == null ? " " : Arrstr[1], font, drawbrush, new Rectangle(X, Y, LineWeight, LineHeight), tempstrFormat); } else { StringFormat tempstrFormat = new StringFormat(); tempstrFormat.LineAlignment = strFormat.LineAlignment; if (Temp.Length == 2) { if (Temp[0].Substring(0, 1) == "R") { tempstrFormat.Alignment = StringAlignment.Far; g.DrawString(Temp[1] == null ? " " : Temp[1], font, drawbrush, new Rectangle(X, Y, LineWeight, LineHeight), tempstrFormat); } else if (Temp[0].Substring(0, 1) == "L") { tempstrFormat.Alignment = StringAlignment.Near; g.DrawString(Temp[1] == null ? " " : Temp[1], font, drawbrush, new Rectangle(X, Y, LineWeight, LineHeight), tempstrFormat); } else { tempstrFormat.Alignment = StringAlignment.Center; g.DrawString(Temp[1] == null ? " " : Temp[1], font, drawbrush, new Rectangle(X, Y, LineWeight, LineHeight), tempstrFormat); } } else if (Temp.Length == 3) { drawbrush = new SolidBrush((Color)ColorTranslator.FromHtml(Temp[1])); if (Temp[0].Substring(0, 1) == "R") { tempstrFormat.Alignment = StringAlignment.Far; g.DrawString(Temp[2] == null ? " " : Temp[2], font, drawbrush, new Rectangle(X, Y, LineWeight, LineHeight), tempstrFormat); } else if (Temp[0].Substring(0, 1) == "L") { tempstrFormat.Alignment = StringAlignment.Near; g.DrawString(Temp[2] == null ? " " : Temp[2], font, drawbrush, new Rectangle(X, Y, LineWeight, LineHeight), tempstrFormat); } else { tempstrFormat.Alignment = StringAlignment.Center; g.DrawString(Temp[2] == null ? " " : Temp[2], font, drawbrush, new Rectangle(X, Y, LineWeight, LineHeight), tempstrFormat); } } } } else if (Arrstr[0].Substring(0, 1) == "P") { if (Arrstr[0].Length != 13) return; int X = int.Parse(Arrstr[0].Substring(1, 3)); int Y = int.Parse(Arrstr[0].Substring(4, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(7, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(10, 3)); if (Arrstr[1] == null) return; if (!System.IO.File.Exists(Arrstr[1])) { return; } Image img = Image.FromFile(Arrstr[1]); g.DrawImage(img, new Rectangle(X, Y, LineWeight, LineHeight)); } else if (Arrstr[0].Substring(0, 1) == "B") //一维码 { if (Arrstr[0].Length != 13) return; int X = int.Parse(Arrstr[0].Substring(1, 3)); int Y = int.Parse(Arrstr[0].Substring(4, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(7, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(10, 3)); if (Arrstr[1] == null) return; //Image img = Image.FromFile(Arrstr[1]); g.DrawImage(GetBarcodeBitmap1(Arrstr[1], LineWeight, LineHeight), new Rectangle(X, Y, LineWeight, LineHeight)); } else if (Arrstr[0].Substring(0, 1) == "L") //画线 { if (Arrstr[0].Length != 13) return; int X1 = int.Parse(Arrstr[0].Substring(1, 3)); int Y1 = int.Parse(Arrstr[0].Substring(4, 3)); int X2 = int.Parse(Arrstr[0].Substring(7, 3)); int Y2 = int.Parse(Arrstr[0].Substring(10, 3)); Pen pen = new Pen(Color.Black); try { pen.Width = int.Parse(Arrstr[1]); } catch (Exception a) { } g.DrawLine(pen, X1, Y1, X2, Y2); } else if (Arrstr[0].Substring(0, 1) == "Q") //二维码 { if (Arrstr[0].Length != 13) return; int X = int.Parse(Arrstr[0].Substring(1, 3)); int Y = int.Parse(Arrstr[0].Substring(4, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(7, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(10, 3)); if (Arrstr[1] == null) return; g.DrawImage(GetQRCode(Arrstr[1], LineWeight, LineHeight), new Rectangle(X, Y, LineWeight, LineHeight)); } else if (Arrstr[0].Substring(0, 1) == "V") //加载图片 { if (Arrstr[0].Length != 13) return; int X = int.Parse(Arrstr[0].Substring(1, 3)); int Y = int.Parse(Arrstr[0].Substring(4, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(7, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(10, 3)); if (Arrstr[1] == null) return; Image img; byte[] bytearr = Convert.FromBase64String(Arrstr[1]); using (MemoryStream ms = new MemoryStream(bytearr)) { img = Image.FromStream(ms); } Bitmap binaryImage = new Bitmap(img.Width, img.Height); // 对图像进行二值化处理 抹除杂乱点 using (Graphics g1 = Graphics.FromImage(binaryImage)) { g1.DrawImage(img, 0, 0); // 将图像转换为黑白 for (int y = 0; y < binaryImage.Height; y++) { for (int x = 0; x < binaryImage.Width; x++) { Color pixelColor = binaryImage.GetPixel(x, y); int averageColor = (pixelColor.R + pixelColor.G + pixelColor.B) / 3; Color newColor = averageColor < 128 ? Color.Black : Color.White; binaryImage.SetPixel(x, y, newColor); } } } g.DrawImage(binaryImage, new Rectangle(X, Y, LineWeight, LineHeight)); } else if (Arrstr[0].Substring(0, 1) == "D")//画背景DXXXYYYWWWHHH|FF0000 { if (Arrstr[0].Length != 13) return; int X = int.Parse(Arrstr[0].Substring(1, 3)); int Y = int.Parse(Arrstr[0].Substring(4, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(7, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(10, 3)); if (Arrstr[1] == null) return; SolidBrush brush = new SolidBrush((Color)ColorTranslator.FromHtml(Arrstr[1])); g.FillRectangle(brush, new Rectangle(X, Y, LineWeight, LineHeight)); } } /// /// 生成条形码 /// /// 需要生成条码的内容 /// 条码宽度 /// 条码长度 /// 返回条码图形 public static Bitmap GetBarcodeBitmap(string barcodeContent, int barcodeWidth, int barcodeHeight) { BarcodeWriter barcodeWriter = new BarcodeWriter(); barcodeWriter.Format = BarcodeFormat.CODE_128;//设置编码格式 EncodingOptions encodingOptions = new EncodingOptions(); encodingOptions.Width = barcodeWidth;//设置宽度 encodingOptions.Height = barcodeHeight;//设置长度 encodingOptions.Margin = 0;//设置边距 encodingOptions.PureBarcode = true; barcodeWriter.Options = encodingOptions; Bitmap bitmap = barcodeWriter.Write(barcodeContent); return bitmap; } public static Bitmap GetBarcodeBitmap1(string code, int width, int height) { BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.IncludeLabel = false; b.Alignment = BarcodeLib.AlignmentPositions.CENTER; b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置 //System.Drawing.Font font = new System.Drawing.Font("Arial", 25, FontStyle.Bold);//字体设置 //b.LabelFont = font; // b.BarWidth = 6; Image Image = b.Encode(BarcodeLib.TYPE.CODE128, code, System.Drawing.Color.Black, System.Drawing.Color.White, width, height); Bitmap bitmap = new Bitmap(Image); return bitmap; } public string ToHexStrFromByte(byte[] byteDatas) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < byteDatas.Length; i++) { builder.Append(string.Format("{0:X2} ", byteDatas[i])); } return builder.ToString().Trim(); } public static Bitmap GetQRCode(string text, int width, int height) { BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; QrCodeEncodingOptions options = new QrCodeEncodingOptions() { DisableECI = true,//设置内容编码 CharacterSet = "UTF-8", Width = width,//设置二维码的宽度和高度 Height = height, Margin = 1//设置二维码的边距,单位不是固定像素 }; writer.Options = options; Bitmap map = writer.Write(text); return map; } } }