BarcodeBatchSet.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using Curtain.Framework.Barcode.OneD;
  8. using Curtain.Framework.Barcode.QRCode;
  9. using Curtain.Helpers;
  10. using Microsoft.SqlServer.Server;
  11. namespace Dongke.IBOSS.PRD.WCF.DataModels.BarcodeBatchSet
  12. {
  13. public class BarcodeBatchConfig_Range
  14. {
  15. public float X;
  16. public float Y;
  17. public float W;
  18. public float H;
  19. }
  20. public class BarcodeBatchConfig_Font
  21. {
  22. public string FamilyName;
  23. public float Size;
  24. public FontStyle Style;
  25. public GraphicsUnit Gunit;
  26. }
  27. public class BarcodeBatchConfig_TXT
  28. {
  29. public int ID;
  30. public string Name;
  31. public bool IsShow = true;
  32. public string EG;
  33. [NonSerialized]
  34. public string P_TXT;
  35. public RectangleF Range;
  36. [NonSerialized]
  37. public Rectangle RangePixel;
  38. [NonSerialized]
  39. public Rectangle RangePixelStr;
  40. //[NonSerialized]
  41. //public Rectangle RangePixelAngle;
  42. public string AH = "居中";
  43. public string AV = "居中";
  44. public string Angle = "0";
  45. public BarcodeBatchConfig_Font TxtFont;
  46. [NonSerialized]
  47. public Font DrawFont;
  48. [NonSerialized]
  49. public StringFormat DrawFormat;
  50. public bool Clip = true;
  51. public bool Wrap = true;
  52. public override string ToString()
  53. {
  54. return $"文本位置{ID}:{Name}";
  55. }
  56. }
  57. public class BarcodeBatchConfig_BC
  58. {
  59. public string EG;
  60. [NonSerialized]
  61. public string P_TXT;
  62. public RectangleF Range;
  63. [NonSerialized]
  64. public Rectangle RangePixel;
  65. //[NonSerialized]
  66. //public Rectangle RangePixel;
  67. public string Type;
  68. public string Format;
  69. public string Level;
  70. }
  71. public class BarcodeBatchConfig
  72. {
  73. public float HB_W;
  74. public float HB_H;
  75. public bool FlipH;
  76. public float PageW;
  77. public float PageH;
  78. public int PageRowNum;
  79. public int PageColNum;
  80. public bool ShowGrid;
  81. public float DpiX;
  82. public float DpiY;
  83. [NonSerialized]
  84. public Rectangle RangePixel;
  85. public BarcodeBatchConfig_BC BC;
  86. public Dictionary<int, BarcodeBatchConfig_TXT> TXT_S;
  87. }
  88. public class BarcodeBatchData
  89. {
  90. public string Barcode;
  91. public Dictionary<int, string> TXT_S;
  92. }
  93. public class BarcodeBatchPrint
  94. {
  95. public static Pen PEN_Grid = new Pen(Color.Black, 1);
  96. static BarcodeBatchPrint()
  97. {
  98. PEN_Grid.DashStyle = DashStyle.Dash;
  99. }
  100. /// <summary>
  101. /// 毫米和像素的转换系数
  102. /// </summary>
  103. public const decimal MILLIMETER_PER_INCH = 25.4m;
  104. public static int MillimeterToPixel(float mm, float dpi)
  105. {
  106. decimal pixel = Convert.ToDecimal(mm) / MILLIMETER_PER_INCH * Convert.ToDecimal(dpi);
  107. return Convert.ToInt32(Math.Round(pixel, 0));
  108. }
  109. public static Rectangle MillimeterToPixel(RectangleF rectF, float dpiX, float dpiY)
  110. {
  111. Rectangle rect = new Rectangle();
  112. rect.X = MillimeterToPixel(rectF.X, dpiX);
  113. rect.Y = MillimeterToPixel(rectF.Y, dpiY);
  114. rect.Width = MillimeterToPixel(rectF.Width, dpiX);
  115. rect.Height = MillimeterToPixel(rectF.Height, dpiY);
  116. return rect;
  117. }
  118. public static Font DoToFont(BarcodeBatchConfig_Font txtFont)
  119. {
  120. //return new Font(txtFont.FamilyName, txtFont.Size, txtFont.Style, txtFont.Gunit);
  121. return new Font(txtFont.FamilyName, txtFont.Size, txtFont.Style);
  122. }
  123. public static BarcodeBatchConfig DoToPixel(BarcodeBatchConfig BBC)
  124. {
  125. BBC.RangePixel = new Rectangle();
  126. BBC.RangePixel.X = MillimeterToPixel(BBC.HB_W, BBC.DpiX);
  127. BBC.RangePixel.Y = MillimeterToPixel(BBC.HB_H, BBC.DpiY);
  128. BBC.RangePixel.Width = MillimeterToPixel(BBC.PageW, BBC.DpiX);
  129. BBC.RangePixel.Height = MillimeterToPixel(BBC.PageH, BBC.DpiY);
  130. BBC.BC.RangePixel = MillimeterToPixel(BBC.BC.Range, BBC.DpiX, BBC.DpiY);
  131. foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values)
  132. {
  133. item.RangePixel = MillimeterToPixel(item.Range, BBC.DpiX, BBC.DpiY);
  134. item.DrawFont = DoToFont(item.TxtFont);
  135. item.DrawFormat = new StringFormat(StringFormat.GenericTypographic);
  136. switch (item.AH)
  137. {
  138. case "居左":
  139. item.DrawFormat.Alignment = StringAlignment.Near;
  140. break;
  141. case "居右":
  142. item.DrawFormat.Alignment = StringAlignment.Far;
  143. break;
  144. default:
  145. item.DrawFormat.Alignment = StringAlignment.Center;
  146. break;
  147. }
  148. switch (item.AV)
  149. {
  150. case "顶端":
  151. item.DrawFormat.LineAlignment = StringAlignment.Near;
  152. break;
  153. case "底端":
  154. item.DrawFormat.LineAlignment = StringAlignment.Far;
  155. break;
  156. default:
  157. item.DrawFormat.LineAlignment = StringAlignment.Center;
  158. break;
  159. }
  160. item.DrawFormat.FormatFlags = StringFormatFlags.NoFontFallback;
  161. if (!item.Clip)
  162. {
  163. item.DrawFormat.FormatFlags = item.DrawFormat.FormatFlags | StringFormatFlags.NoClip;
  164. }
  165. if (!item.Wrap)
  166. {
  167. item.DrawFormat.FormatFlags = item.DrawFormat.FormatFlags | StringFormatFlags.NoWrap;
  168. }
  169. item.RangePixelStr = item.RangePixel;
  170. item.RangePixelStr.X = 0;
  171. item.RangePixelStr.Y = 0;
  172. if (item.Angle == "90" || item.Angle == "270")
  173. {
  174. item.RangePixelStr.Width = item.RangePixel.Height;
  175. item.RangePixelStr.Height = item.RangePixel.Width;
  176. }
  177. /*if (item.Angle == "90")
  178. {
  179. Rectangle r = item.RangePixel;
  180. item.RangePixel.X = r.X + r.Width;
  181. item.RangePixel.Y = r.Y;
  182. item.RangePixel.Width = r.Height;
  183. item.RangePixel.Height = r.Width;
  184. }
  185. else if (item.Angle == "180")
  186. {
  187. Rectangle r = item.RangePixel;
  188. item.RangePixel.X = r.X + r.Width;
  189. item.RangePixel.Y = r.Y + r.Height;
  190. }
  191. else if (item.Angle == "270")
  192. {
  193. Rectangle r = item.RangePixel;
  194. item.RangePixel.X = r.X;
  195. item.RangePixel.Y = r.Y + r.Height;
  196. item.RangePixel.Width = r.Height;
  197. item.RangePixel.Height = r.Width;
  198. }*/
  199. }
  200. return BBC;
  201. }
  202. public static BarcodeBatchConfig DoConfig(BarcodeBatchConfig BBC, DataTable layoutItem, DataRow BBD)
  203. {
  204. if (BBD != null)
  205. {
  206. BBC.BC.P_TXT = null;
  207. foreach (BarcodeBatchConfig_TXT txt in BBC.TXT_S.Values)
  208. {
  209. txt.P_TXT = txt.EG + "";
  210. }
  211. foreach (DataRow item in layoutItem.Rows)
  212. {
  213. string item_code = item["ITEMCODE"] + "";
  214. string col_code = item["COL_CODE"] + "";
  215. string item_type = item["ITEMTYPE"] + "";
  216. //string item_value = item["ITEMSAMPLE"] + "";
  217. if (BBD.Table.Columns.Contains(col_code))
  218. {
  219. object col_value = BBD[col_code];
  220. try
  221. {
  222. if (item_type == "DATE")
  223. {
  224. col_value = Convert.ToDateTime(col_value).ToString("yyyy-MM-dd");
  225. }
  226. }
  227. catch
  228. {
  229. col_value = null;
  230. }
  231. string item_value = col_value + "";
  232. if (item_code == "{#产品条码#}")
  233. {
  234. BBC.BC.P_TXT = item_value;
  235. }
  236. foreach (BarcodeBatchConfig_TXT txt in BBC.TXT_S.Values)
  237. {
  238. txt.P_TXT = txt.P_TXT.Replace(item_code, item_value);
  239. }
  240. }
  241. }
  242. /*BBC.BC.P_TXT = BBD.Barcode;
  243. if (BBD.TXT_S != null && BBD.TXT_S.Count > 0)
  244. {
  245. if (BBD.TXT_S.Count == 1)
  246. {
  247. string txt = BBD.TXT_S[1];
  248. foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values)
  249. {
  250. item.P_TXT = txt;
  251. }
  252. }
  253. else
  254. {
  255. foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values)
  256. {
  257. item.P_TXT = "";
  258. }
  259. foreach (int item in BBD.TXT_S.Keys)
  260. {
  261. if (BBC.TXT_S.ContainsKey(item))
  262. {
  263. BBC.TXT_S[item].P_TXT = BBD.TXT_S[item];
  264. }
  265. }
  266. }
  267. }*/
  268. }
  269. return BBC;
  270. }
  271. public static Image CreateImageHB(BarcodeBatchConfig BBC, DataTable layoutItem, DataRow BBD)
  272. {
  273. BBC = DoConfig(BBC, layoutItem, BBD);
  274. //int w = MillimeterToPixel(BBC.HB_W, BBC.DpiX);
  275. //int h = MillimeterToPixel(BBC.HB_H, BBC.DpiY);
  276. Bitmap imageHB = new Bitmap(BBC.RangePixel.X, BBC.RangePixel.Y);
  277. imageHB.SetResolution(BBC.DpiX, BBC.DpiY);
  278. using (Graphics imageG = Graphics.FromImage(imageHB))
  279. {
  280. //imageG.PageUnit = GraphicsUnit.Millimeter;
  281. imageG.PageUnit = GraphicsUnit.Pixel;
  282. // 条码
  283. //Image barcodeImage = null;
  284. try
  285. {
  286. if (BBC.BC.Type == "一维码")
  287. {
  288. OneDDrawingOptions op = new OneDDrawingOptions
  289. {
  290. ImageRect = BBC.BC.RangePixel,//MillimeterToPixel(BBC.BC.Range, BBC.DpiX, BBC.DpiY),
  291. DrawGraphics = imageG,
  292. BackColor = Color.Transparent
  293. };
  294. OneDFormat format = (OneDFormat)Enum.Parse(typeof(OneDFormat), BBC.BC.Format);
  295. //barcodeImage = OneDHelper.GetOneDImage(BBC.BC.EG, op, format);
  296. OneDHelper.DrawOneD(BBC.BC.P_TXT, op, format);
  297. }
  298. else if (BBC.BC.Type == "二维码")
  299. {
  300. QRCodeEncodingOptions eop = new QRCodeEncodingOptions
  301. {
  302. ECLevel = (QRECLevel)Enum.Parse(typeof(QRECLevel), BBC.BC.Level)
  303. };
  304. QRCodeDrawingOptions op = new QRCodeDrawingOptions();
  305. op.ImageRect = BBC.BC.RangePixel;//MillimeterToPixel(BBC.BC.Range, BBC.DpiX, BBC.DpiY);
  306. op.DrawGraphics = imageG;
  307. op.BackColor = Color.Transparent;
  308. //barcodeImage = QRCodeHelper.GetQRCodeImage(BBC.BC.EG, op, eop);
  309. QRCodeHelper.DrawQRCode(BBC.BC.P_TXT, op, eop);
  310. }
  311. }
  312. catch (ArgumentOutOfRangeException ex)
  313. {
  314. //MessageBox.Show("高度或宽度不够");
  315. //return null;
  316. }
  317. catch (Exception ex)
  318. {
  319. //MessageBox.Show("条码生成异常");
  320. //return null;
  321. }
  322. imageG.DrawRectangle(Pens.Blue, BBC.BC.RangePixel);
  323. foreach (BarcodeBatchConfig_TXT item in BBC.TXT_S.Values)
  324. {
  325. if (item is null || !item.IsShow)
  326. {
  327. continue;
  328. }
  329. /*Matrix mtxSave = null;
  330. if (item.Angle == "90")
  331. {
  332. mtxSave = imageG.Transform;
  333. Matrix mtxRotate = imageG.Transform;
  334. mtxRotate.RotateAt(90, item.RangePixel.Location);
  335. imageG.Transform = mtxRotate;
  336. }
  337. else if (item.Angle == "180")
  338. {
  339. mtxSave = imageG.Transform;
  340. Matrix mtxRotate = imageG.Transform;
  341. mtxRotate.RotateAt(180, item.RangePixel.Location);
  342. imageG.Transform = mtxRotate;
  343. }
  344. else if (item.Angle == "270")
  345. {
  346. mtxSave = imageG.Transform;
  347. Matrix mtxRotate = imageG.Transform;
  348. mtxRotate.RotateAt(270, item.RangePixel.Location);
  349. imageG.Transform = mtxRotate;
  350. }*/
  351. imageG.DrawRectangle(Pens.Red, item.RangePixel);
  352. if (string.IsNullOrWhiteSpace(item.P_TXT))
  353. {
  354. continue;
  355. }
  356. if (item.Angle == "0")
  357. {
  358. imageG.DrawString(item.P_TXT, item.DrawFont, Brushes.Black, item.RangePixel, item.DrawFormat);
  359. }
  360. else
  361. {
  362. using (Bitmap imageStr = new Bitmap(item.RangePixelStr.Width, item.RangePixelStr.Height))
  363. {
  364. if (item.Angle == "90" || item.Angle == "270")
  365. {
  366. imageStr.SetResolution(BBC.DpiY, BBC.DpiX);
  367. }
  368. else
  369. {
  370. imageStr.SetResolution(BBC.DpiX, BBC.DpiY);
  371. }
  372. using (Graphics imageStrG = Graphics.FromImage(imageStr))
  373. {
  374. imageStrG.DrawString(item.P_TXT, item.DrawFont, Brushes.Black, item.RangePixelStr, item.DrawFormat);
  375. }
  376. if (item.Angle == "90")
  377. {
  378. imageStr.RotateFlip(RotateFlipType.Rotate90FlipNone);
  379. }
  380. else if (item.Angle == "180")
  381. {
  382. imageStr.RotateFlip(RotateFlipType.Rotate180FlipNone);
  383. }
  384. else if (item.Angle == "270")
  385. {
  386. imageStr.RotateFlip(RotateFlipType.Rotate270FlipNone);
  387. }
  388. imageG.DrawImage(imageStr, item.RangePixel);
  389. }
  390. }
  391. //imageG.DrawString(item.EG, item.DrawFont, Brushes.Black, item.RangePixel, item.DrawFormat);
  392. /*if (mtxSave != null)
  393. {
  394. imageG.Transform = mtxSave;
  395. }*/
  396. }
  397. }
  398. if (BBC.FlipH)
  399. {
  400. imageHB.RotateFlip(RotateFlipType.RotateNoneFlipX);
  401. }
  402. return imageHB;
  403. }
  404. public static Image ShowImagePage(BarcodeBatchConfig BBC)
  405. {
  406. Bitmap imagePage = new Bitmap(BBC.RangePixel.Width, BBC.RangePixel.Height);
  407. imagePage.SetResolution(BBC.DpiX, BBC.DpiY);
  408. int x = 0;
  409. int y = 0;
  410. using (Graphics imageG = Graphics.FromImage(imagePage))
  411. {
  412. imageG.PageUnit = GraphicsUnit.Pixel;
  413. for (int row = 0; row < BBC.PageRowNum; row++)
  414. {
  415. y = row * BBC.RangePixel.Y;
  416. for (int col = 0; col < BBC.PageColNum; col++)
  417. {
  418. x = col * BBC.RangePixel.X;
  419. using (Image br = CreateImageHB(BBC, null, null))
  420. {
  421. //imageG.DrawRectangle(Pens.Red, x, y, BBC.RangePixel.X, BBC.RangePixel.Y);
  422. imageG.DrawImage(br, x, y);
  423. }
  424. if (BBC.ShowGrid)
  425. {
  426. // 画横线
  427. if (col == 0)
  428. {
  429. imageG.DrawLine(PEN_Grid, 0, y, BBC.RangePixel.Width, y);
  430. }
  431. // 画竖线
  432. if (row == 0)
  433. {
  434. imageG.DrawLine(PEN_Grid, x, 0, x, BBC.RangePixel.Height);
  435. }
  436. }
  437. }
  438. }
  439. if (BBC.ShowGrid)
  440. {
  441. x += BBC.RangePixel.X;
  442. y += BBC.RangePixel.Y;
  443. imageG.DrawLine(PEN_Grid, 0, y, BBC.RangePixel.Width, y);
  444. imageG.DrawLine(PEN_Grid, x, 0, x, BBC.RangePixel.Height);
  445. }
  446. }
  447. return imagePage;
  448. }
  449. public static Image CreateImagePage(BarcodeBatchConfig BBC, DataTable layoutItem, List<DataRow> BBD_S, int index, out int nextIndex)
  450. {
  451. nextIndex = -1;
  452. if (BBD_S == null)
  453. {
  454. return null;
  455. }
  456. int count = BBD_S.Count;
  457. if (count == 0 || index >= count)
  458. {
  459. return null;
  460. }
  461. nextIndex = index;
  462. Bitmap imagePage = new Bitmap(BBC.RangePixel.Width, BBC.RangePixel.Height);
  463. imagePage.SetResolution(BBC.DpiX, BBC.DpiY);
  464. int x = 0;
  465. int y = 0;
  466. using (Graphics imageG = Graphics.FromImage(imagePage))
  467. {
  468. imageG.PageUnit = GraphicsUnit.Pixel;
  469. for (int row = 0; row < BBC.PageRowNum; row++)
  470. {
  471. y = row * BBC.RangePixel.Y;
  472. for (int col = 0; col < BBC.PageColNum; col++)
  473. {
  474. x = col * BBC.RangePixel.X;
  475. using (Image br = CreateImageHB(BBC, layoutItem, BBD_S[nextIndex]))
  476. {
  477. //imageG.DrawRectangle(Pens.Red, x, y, BBC.RangePixel.X, BBC.RangePixel.Y);
  478. imageG.DrawImage(br, x, y);
  479. }
  480. nextIndex++;
  481. if (nextIndex >= count)
  482. {
  483. break;
  484. }
  485. }
  486. if (nextIndex >= count)
  487. {
  488. break;
  489. }
  490. }
  491. if (BBC.ShowGrid)
  492. {
  493. for (int row = 0; row <= BBC.PageRowNum; row++)
  494. {
  495. y = row * BBC.RangePixel.Y;
  496. imageG.DrawLine(PEN_Grid, 0, y, BBC.RangePixel.Width, y);
  497. }
  498. for (int col = 0; col <= BBC.PageColNum; col++)
  499. {
  500. x = col * BBC.RangePixel.X;
  501. imageG.DrawLine(PEN_Grid, x, 0, x, BBC.RangePixel.Height);
  502. }
  503. }
  504. }
  505. return imagePage;
  506. }
  507. }
  508. }