using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using Curtain.Framework.Barcode.OneD; using Curtain.Framework.Barcode.QRCode; using Curtain.Helpers; using Microsoft.SqlServer.Server; namespace Dongke.IBOSS.PRD.WCF.DataModels.BarcodeBatchSet { public class BarcodeBatchConfig_Range { public float X; public float Y; public float W; public float H; } public class BarcodeBatchConfig_Font { public string FamilyName; public float Size; public FontStyle Style; public GraphicsUnit Gunit; [NonSerialized] public Font DrawFont; } public class BarcodeBatchConfig_TXT { public int ID; public string Name; public bool IsShow = true; public string EG; [NonSerialized] public string P_TXT; public RectangleF Range; [NonSerialized] public Rectangle RangePixel; [NonSerialized] public Rectangle RangePixelStr; //[NonSerialized] //public Rectangle RangePixelAngle; public string AH = "居中"; public string AV = "居中"; public string Angle = "0"; public BarcodeBatchConfig_Font TxtFont; [NonSerialized] public StringFormat DrawFormat; public bool Clip = true; public bool Wrap = true; public override string ToString() { return $"文本位置{ID}:{Name}"; } } public class BarcodeBatchConfig_BC { public string EG; [NonSerialized] public string P_TXT; public RectangleF Range; [NonSerialized] public Rectangle RangePixel; //[NonSerialized] //public Rectangle RangePixel; public string Type; public string Format; public string Level; public BarcodeBatchConfig_Font TxtFont; } public class BarcodeBatchConfig { public float HB_W; public float HB_H; public bool FlipH; public float PageW; public float PageH; public int PageRowNum; public int PageColNum; public bool ShowGrid; public float DpiX; public float DpiY; [NonSerialized] public Rectangle RangePixel; public BarcodeBatchConfig_BC BC; public Dictionary TXT_S; } public class BarcodeBatchData { public string Barcode; public Dictionary TXT_S; } public class BarcodeBatchPrint { public static Pen PEN_Grid = new Pen(Color.Black, 1); static BarcodeBatchPrint() { PEN_Grid.DashStyle = DashStyle.Dash; } /// /// 毫米和像素的转换系数 /// public const decimal MILLIMETER_PER_INCH = 25.4m; public static int MillimeterToPixel(float mm, float dpi) { decimal pixel = Convert.ToDecimal(mm) / MILLIMETER_PER_INCH * Convert.ToDecimal(dpi); return Convert.ToInt32(Math.Round(pixel, 0)); } public static Rectangle MillimeterToPixel(RectangleF rectF, float dpiX, float dpiY) { Rectangle rect = new Rectangle(); rect.X = MillimeterToPixel(rectF.X, dpiX); rect.Y = MillimeterToPixel(rectF.Y, dpiY); rect.Width = MillimeterToPixel(rectF.Width, dpiX); rect.Height = MillimeterToPixel(rectF.Height, dpiY); return rect; } public static Font DoToFont(BarcodeBatchConfig_Font txtFont) { if (txtFont == null) { return new Font("宋体", 9f); } //return new Font(txtFont.FamilyName, txtFont.Size, txtFont.Style, txtFont.Gunit); return new Font(txtFont.FamilyName, txtFont.Size, txtFont.Style); } public static BarcodeBatchConfig DoToPixel(BarcodeBatchConfig BBC) { BBC.RangePixel = new Rectangle(); BBC.RangePixel.X = MillimeterToPixel(BBC.HB_W, BBC.DpiX); BBC.RangePixel.Y = MillimeterToPixel(BBC.HB_H, BBC.DpiY); BBC.RangePixel.Width = MillimeterToPixel(BBC.PageW, BBC.DpiX); BBC.RangePixel.Height = MillimeterToPixel(BBC.PageH, BBC.DpiY); BBC.BC.RangePixel = MillimeterToPixel(BBC.BC.Range, BBC.DpiX, BBC.DpiY); if (BBC.BC.TxtFont == null) { BBC.BC.TxtFont = new BarcodeBatchConfig_Font(); BBC.BC.TxtFont.FamilyName = "宋体"; BBC.BC.TxtFont.Size = 9; BBC.BC.TxtFont.Style = FontStyle.Regular; } BBC.BC.TxtFont.DrawFont = DoToFont(BBC.BC.TxtFont); foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values) { item.RangePixel = MillimeterToPixel(item.Range, BBC.DpiX, BBC.DpiY); if (item.TxtFont == null) { item.TxtFont = new BarcodeBatchConfig_Font(); item.TxtFont.FamilyName = "宋体"; item.TxtFont.Size = 9; item.TxtFont.Style = FontStyle.Regular; } item.TxtFont.DrawFont = DoToFont(item.TxtFont); item.DrawFormat = new StringFormat(StringFormat.GenericTypographic); switch (item.AH) { case "居左": item.DrawFormat.Alignment = StringAlignment.Near; break; case "居右": item.DrawFormat.Alignment = StringAlignment.Far; break; default: item.DrawFormat.Alignment = StringAlignment.Center; break; } switch (item.AV) { case "顶端": item.DrawFormat.LineAlignment = StringAlignment.Near; break; case "底端": item.DrawFormat.LineAlignment = StringAlignment.Far; break; default: item.DrawFormat.LineAlignment = StringAlignment.Center; break; } item.DrawFormat.FormatFlags = StringFormatFlags.NoFontFallback; if (!item.Clip) { item.DrawFormat.FormatFlags = item.DrawFormat.FormatFlags | StringFormatFlags.NoClip; } if (!item.Wrap) { item.DrawFormat.FormatFlags = item.DrawFormat.FormatFlags | StringFormatFlags.NoWrap; } item.RangePixelStr = item.RangePixel; item.RangePixelStr.X = 0; item.RangePixelStr.Y = 0; if (item.Angle == "90" || item.Angle == "270") { item.RangePixelStr.Width = item.RangePixel.Height; item.RangePixelStr.Height = item.RangePixel.Width; } /*if (item.Angle == "90") { Rectangle r = item.RangePixel; item.RangePixel.X = r.X + r.Width; item.RangePixel.Y = r.Y; item.RangePixel.Width = r.Height; item.RangePixel.Height = r.Width; } else if (item.Angle == "180") { Rectangle r = item.RangePixel; item.RangePixel.X = r.X + r.Width; item.RangePixel.Y = r.Y + r.Height; } else if (item.Angle == "270") { Rectangle r = item.RangePixel; item.RangePixel.X = r.X; item.RangePixel.Y = r.Y + r.Height; item.RangePixel.Width = r.Height; item.RangePixel.Height = r.Width; }*/ } return BBC; } public static BarcodeBatchConfig DoConfig(BarcodeBatchConfig BBC, DataTable layoutItem, DataRow BBD) { if (BBD != null) { BBC.BC.P_TXT = null; foreach (BarcodeBatchConfig_TXT txt in BBC.TXT_S.Values) { txt.P_TXT = txt.EG + ""; } foreach (DataRow item in layoutItem.Rows) { string item_code = item["ITEMCODE"] + ""; string col_code = item["COL_CODE"] + ""; string item_type = item["ITEMTYPE"] + ""; //string item_value = item["ITEMSAMPLE"] + ""; if (BBD.Table.Columns.Contains(col_code)) { object col_value = BBD[col_code]; try { if (item_type == "DATE") { col_value = Convert.ToDateTime(col_value).ToString("yyyy-MM-dd"); } } catch { col_value = null; } string item_value = col_value + ""; if (item_code == "{#产品条码#}") { BBC.BC.P_TXT = item_value; } foreach (BarcodeBatchConfig_TXT txt in BBC.TXT_S.Values) { txt.P_TXT = txt.P_TXT.Replace(item_code, item_value); } } } /*BBC.BC.P_TXT = BBD.Barcode; if (BBD.TXT_S != null && BBD.TXT_S.Count > 0) { if (BBD.TXT_S.Count == 1) { string txt = BBD.TXT_S[1]; foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values) { item.P_TXT = txt; } } else { foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values) { item.P_TXT = ""; } foreach (int item in BBD.TXT_S.Keys) { if (BBC.TXT_S.ContainsKey(item)) { BBC.TXT_S[item].P_TXT = BBD.TXT_S[item]; } } } }*/ } return BBC; } public static Image CreateImageHB(BarcodeBatchConfig BBC, DataTable layoutItem, DataRow BBD) { BBC = DoConfig(BBC, layoutItem, BBD); //int w = MillimeterToPixel(BBC.HB_W, BBC.DpiX); //int h = MillimeterToPixel(BBC.HB_H, BBC.DpiY); Bitmap imageHB = new Bitmap(BBC.RangePixel.X, BBC.RangePixel.Y); imageHB.SetResolution(BBC.DpiX, BBC.DpiY); using (Graphics imageG = Graphics.FromImage(imageHB)) { //imageG.PageUnit = GraphicsUnit.Millimeter; imageG.PageUnit = GraphicsUnit.Pixel; // 条码 //Image barcodeImage = null; try { if (BBC.BC.Type == "一维码") { OneDDrawingOptions op = new OneDDrawingOptions { ImageRect = BBC.BC.RangePixel,//MillimeterToPixel(BBC.BC.Range, BBC.DpiX, BBC.DpiY), DrawGraphics = imageG, BackColor = Color.Transparent }; OneDFormat format = (OneDFormat)Enum.Parse(typeof(OneDFormat), BBC.BC.Format); if (format == OneDFormat.EAN_13) { op.ShowText = true; op.TextFont = BBC.BC.TxtFont.DrawFont; } //barcodeImage = OneDHelper.GetOneDImage(BBC.BC.EG, op, format); OneDHelper.DrawOneD(BBC.BC.P_TXT, op, format); } else if (BBC.BC.Type == "二维码") { QRCodeEncodingOptions eop = new QRCodeEncodingOptions { ECLevel = (QRECLevel)Enum.Parse(typeof(QRECLevel), BBC.BC.Level) }; QRCodeDrawingOptions op = new QRCodeDrawingOptions(); op.ImageRect = BBC.BC.RangePixel;//MillimeterToPixel(BBC.BC.Range, BBC.DpiX, BBC.DpiY); op.DrawGraphics = imageG; op.BackColor = Color.Transparent; //barcodeImage = QRCodeHelper.GetQRCodeImage(BBC.BC.EG, op, eop); QRCodeHelper.DrawQRCode(BBC.BC.P_TXT, op, eop); } } catch (ArgumentOutOfRangeException ex) { //MessageBox.Show("高度或宽度不够"); //return null; } catch (Exception ex) { //MessageBox.Show("条码生成异常"); //return null; } //imageG.DrawRectangle(Pens.Blue, BBC.BC.RangePixel); foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values) { if (item is null || !item.IsShow) { continue; } /*Matrix mtxSave = null; if (item.Angle == "90") { mtxSave = imageG.Transform; Matrix mtxRotate = imageG.Transform; mtxRotate.RotateAt(90, item.RangePixel.Location); imageG.Transform = mtxRotate; } else if (item.Angle == "180") { mtxSave = imageG.Transform; Matrix mtxRotate = imageG.Transform; mtxRotate.RotateAt(180, item.RangePixel.Location); imageG.Transform = mtxRotate; } else if (item.Angle == "270") { mtxSave = imageG.Transform; Matrix mtxRotate = imageG.Transform; mtxRotate.RotateAt(270, item.RangePixel.Location); imageG.Transform = mtxRotate; }*/ //imageG.DrawRectangle(Pens.Red, item.RangePixel); if (string.IsNullOrWhiteSpace(item.P_TXT)) { continue; } if (item.Angle == "0") { imageG.DrawString(item.P_TXT, item.TxtFont.DrawFont, Brushes.Black, item.RangePixel, item.DrawFormat); } else { using (Bitmap imageStr = new Bitmap(item.RangePixelStr.Width, item.RangePixelStr.Height)) { if (item.Angle == "90" || item.Angle == "270") { imageStr.SetResolution(BBC.DpiY, BBC.DpiX); } else { imageStr.SetResolution(BBC.DpiX, BBC.DpiY); } using (Graphics imageStrG = Graphics.FromImage(imageStr)) { imageStrG.DrawString(item.P_TXT, item.TxtFont.DrawFont, Brushes.Black, item.RangePixelStr, item.DrawFormat); } if (item.Angle == "90") { imageStr.RotateFlip(RotateFlipType.Rotate90FlipNone); } else if (item.Angle == "180") { imageStr.RotateFlip(RotateFlipType.Rotate180FlipNone); } else if (item.Angle == "270") { imageStr.RotateFlip(RotateFlipType.Rotate270FlipNone); } imageG.DrawImage(imageStr, item.RangePixel); } } //imageG.DrawString(item.EG, item.DrawFont, Brushes.Black, item.RangePixel, item.DrawFormat); /*if (mtxSave != null) { imageG.Transform = mtxSave; }*/ } } if (BBC.FlipH) { imageHB.RotateFlip(RotateFlipType.RotateNoneFlipX); } return imageHB; } public static Image ShowImagePage(BarcodeBatchConfig BBC) { Bitmap imagePage = new Bitmap(BBC.RangePixel.Width, BBC.RangePixel.Height); imagePage.SetResolution(BBC.DpiX, BBC.DpiY); int x = 0; int y = 0; using (Graphics imageG = Graphics.FromImage(imagePage)) { imageG.PageUnit = GraphicsUnit.Pixel; for (int row = 0; row < BBC.PageRowNum; row++) { y = row * BBC.RangePixel.Y; for (int col = 0; col < BBC.PageColNum; col++) { x = col * BBC.RangePixel.X; using (Image br = CreateImageHB(BBC, null, null)) { //imageG.DrawRectangle(Pens.Red, x, y, BBC.RangePixel.X, BBC.RangePixel.Y); imageG.DrawImage(br, x, y); } if (BBC.ShowGrid) { // 画横线 if (col == 0) { imageG.DrawLine(PEN_Grid, 0, y, BBC.RangePixel.Width, y); } // 画竖线 if (row == 0) { imageG.DrawLine(PEN_Grid, x, 0, x, BBC.RangePixel.Height); } } } } if (BBC.ShowGrid) { x += BBC.RangePixel.X; y += BBC.RangePixel.Y; imageG.DrawLine(PEN_Grid, 0, y, BBC.RangePixel.Width, y); imageG.DrawLine(PEN_Grid, x, 0, x, BBC.RangePixel.Height); } } return imagePage; } public static Image CreateImagePage(BarcodeBatchConfig BBC, DataTable layoutItem, List BBD_S, int index, out int nextIndex) { nextIndex = -1; if (BBD_S == null) { return null; } int count = BBD_S.Count; if (count == 0 || index >= count) { return null; } nextIndex = index; Bitmap imagePage = new Bitmap(BBC.RangePixel.Width, BBC.RangePixel.Height); imagePage.SetResolution(BBC.DpiX, BBC.DpiY); int x = 0; int y = 0; using (Graphics imageG = Graphics.FromImage(imagePage)) { imageG.PageUnit = GraphicsUnit.Pixel; for (int row = 0; row < BBC.PageRowNum; row++) { y = row * BBC.RangePixel.Y; for (int col = 0; col < BBC.PageColNum; col++) { x = col * BBC.RangePixel.X; using (Image br = CreateImageHB(BBC, layoutItem, BBD_S[nextIndex])) { //imageG.DrawRectangle(Pens.Red, x, y, BBC.RangePixel.X, BBC.RangePixel.Y); imageG.DrawImage(br, x, y); } nextIndex++; if (nextIndex >= count) { break; } } if (nextIndex >= count) { break; } } if (BBC.ShowGrid) { for (int row = 0; row <= BBC.PageRowNum; row++) { y = row * BBC.RangePixel.Y; imageG.DrawLine(PEN_Grid, 0, y, BBC.RangePixel.Width, y); } for (int col = 0; col <= BBC.PageColNum; col++) { x = col * BBC.RangePixel.X; imageG.DrawLine(PEN_Grid, x, 0, x, BBC.RangePixel.Height); } } } return imagePage; } } }