SupportedBarcodes.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using GrapeCity.Documents.Imaging;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Barcode;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15. using DsImagingWeb.Demos.Common;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This example renders samples of all barcode symbologies supported by the Document Solutions Barcode library.
  20. public class SupportedBarcodes
  21. {
  22. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  25. var g = bmp.CreateGraphics(Color.White);
  26. var ms = new MemoryStream();
  27. GcTiffWriter tw = null;
  28. if (targetMime == Util.MimeTypes.TIFF)
  29. tw = new GcTiffWriter(ms);
  30.  
  31. float marginx = dpi, marginy = dpi / 2;
  32. const float padx = 24, pady = 4;
  33. const float gap = 10;
  34. var ip = new PointF(marginx, marginy);
  35.  
  36. var tfCaption = new TextFormat()
  37. {
  38. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  39. FontSize = 14,
  40. };
  41. var tfBarcode = new TextFormat()
  42. {
  43. Font = tfCaption.Font,
  44. FontSize = 6,
  45. };
  46. GcBarcode barcode = new GcBarcode()
  47. {
  48. TextFormat = tfBarcode,
  49. ScaleFactor = 2,
  50. };
  51. barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below;
  52. barcode.Options.SizeOptions.NarrowWideRatio = 0;
  53.  
  54. Action<CodeType, string, string> drawBarcode = (ct_, txt_, txt2_) =>
  55. {
  56. var caption = $"{ct_}:\r\n{txt_}";
  57. if (string.IsNullOrEmpty(txt2_))
  58. barcode.Options.GS1Composite.Type = GS1CompositeType.None;
  59. else
  60. {
  61. caption += $"\r\nDependent CCA: {txt2_}";
  62. barcode.Options.GS1Composite.Type = GS1CompositeType.CCA;
  63. barcode.Options.GS1Composite.Value = txt2_;
  64. }
  65. barcode.Options.CheckSumEnabled = ct_ != CodeType.Code25intlv && ct_ != CodeType.Code_2_of_5 && ct_ != CodeType.Matrix_2_of_5;
  66. var csize = g.MeasureString(caption, tfCaption);
  67. barcode.CodeType = ct_;
  68. barcode.Text = txt_;
  69. var size = g.MeasureBarcode(barcode);
  70. size.Height = Math.Max(size.Height, csize.Height);
  71. var border = new RectangleF(ip, new SizeF(pixelSize.Width - marginx * 2, size.Height + pady * 2));
  72. if (ip.Y + border.Height > pixelSize.Height - marginy)
  73. {
  74. if (tw == null)
  75. return;
  76. tw.AppendFrame(bmp);
  77. bmp.Clear(Color.White);
  78. ip.Y = marginy;
  79. ip = new PointF(marginx, marginy);
  80. border = new RectangleF(ip, border.Size);
  81. }
  82. g.DrawRectangle(border, Color.Gray);
  83. g.DrawString(caption, tfCaption, new PointF(border.Left + padx, border.Top + pady));
  84. g.DrawBarcode(barcode, new RectangleF(border.Right - size.Width - padx, border.Top + pady, size.Width, size.Height));
  85. ip.Y = border.Bottom + gap;
  86. };
  87. //
  88. drawBarcode(CodeType.Ansi39, "*DSBARCODE*", null);
  89. drawBarcode(CodeType.Ansi39x, "*DsImaging*", null);
  90. drawBarcode(CodeType.Codabar, "A12041961D", null);
  91. drawBarcode(CodeType.Code25intlv, "1234567890", null); //#2 Interleaved 2 of 5 (ITF)
  92. drawBarcode(CodeType.Code39, "*DSBARCODE*", null);
  93. drawBarcode(CodeType.Code39x, "*DsImaging*", null);
  94. drawBarcode(CodeType.Code49, "DsBarcode+DsImaging", null);
  95. drawBarcode(CodeType.Code93x, "DsBarcode+DsImaging", null);
  96. drawBarcode(CodeType.Code_93, "DSBARCODE", null);
  97. drawBarcode(CodeType.Code_128_A, "DSIMAGING-2017", null);
  98. drawBarcode(CodeType.Code_128_B, "DSIMAGING-2017", null);
  99. drawBarcode(CodeType.Code_128_C, "1234567890", null);
  100. drawBarcode(CodeType.Code_128auto, "DsImaging-2023", null);
  101. drawBarcode(CodeType.Code_2_of_5, "1234567890", null);
  102. drawBarcode(CodeType.DataMatrix, "DsBarcode+DsImaging", null);
  103. drawBarcode(CodeType.QRCode, "DsBarcode+DsImaging", null);
  104. drawBarcode(CodeType.EAN_8, "1234567", null);
  105. drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", null);
  106. drawBarcode(CodeType.EAN128FNC1, "DsBarcode\nDsImaging", null);
  107. drawBarcode(CodeType.IntelligentMail, "00300999999000000001", null);
  108. drawBarcode(CodeType.JapanesePostal, "TOKYO-10CC-09-1978", null);
  109. drawBarcode(CodeType.PostNet, "152063949", null);
  110. drawBarcode(CodeType.RM4SCC, "SE17PB9Z", null);
  111. drawBarcode(CodeType.Matrix_2_of_5, "1234567890", null);
  112. drawBarcode(CodeType.MSI, "1234567890", null);
  113. drawBarcode(CodeType.MicroPDF417, "DsImaging", null);
  114. drawBarcode(CodeType.Pdf417, "DsImaging", null);
  115. drawBarcode(CodeType.RSS14, "1234567890", null);
  116. drawBarcode(CodeType.RSS14Stacked, "1234567890", null);
  117. drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345");
  118. drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", null);
  119. drawBarcode(CodeType.RSS14Truncated, "1234567890", null);
  120. drawBarcode(CodeType.RSSExpanded, "12345678901234", null);
  121. drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", null);
  122. drawBarcode(CodeType.RSSLimited, "1234567890", null);
  123. drawBarcode(CodeType.RSSLimited, "1234567890", "12345");
  124. drawBarcode(CodeType.UCCEAN128, "DsBarcode+DsImaging", null);
  125. drawBarcode(CodeType.UPC_A, "123456789012", null);
  126. drawBarcode(CodeType.UPC_E0, "123456789012", null);
  127. drawBarcode(CodeType.UPC_E1, "123456789012", null);
  128.  
  129. switch (targetMime)
  130. {
  131. case Common.Util.MimeTypes.TIFF:
  132. tw.AppendFrame(bmp);
  133. tw.Dispose();
  134. break;
  135. case Common.Util.MimeTypes.JPEG:
  136. bmp.SaveAsJpeg(ms);
  137. break;
  138. case Common.Util.MimeTypes.PNG:
  139. bmp.SaveAsPng(ms);
  140. break;
  141. case Common.Util.MimeTypes.BMP:
  142. bmp.SaveAsBmp(ms);
  143. break;
  144. case Common.Util.MimeTypes.GIF:
  145. bmp.SaveAsGif(ms);
  146. break;
  147. case Common.Util.MimeTypes.WEBP:
  148. bmp.SaveAsWebp(ms);
  149. break;
  150. case Common.Util.MimeTypes.ICO:
  151. bmp.SaveAsIco(ms, null, IcoFrameEncoding.Png);
  152. break;
  153. default:
  154. throw new Exception($"Encoding {targetMime} is not supported.");
  155. }
  156. bmp.Dispose();
  157. g.Dispose();
  158. return ms;
  159. }
  160. }
  161. }
  162.