BarcodeBatchSet.cs 21 KB

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