BarcodeDraw.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Runtime.InteropServices;
  5. //using Curtain.Framework.Barcode;
  6. //using Curtain.Framework.Barcode.OneD;
  7. //using Curtain.Framework.Barcode.QRCode;
  8. using Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode;
  9. using Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode.OneD;
  10. using Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode.QRCode;
  11. namespace Dongke.WinForm.Controls.InvoiceLayout
  12. {
  13. public static class BarcodeDraw
  14. {
  15. /// <summary>
  16. /// 绘制图片
  17. /// </summary>
  18. /// <param name="data"></param>
  19. /// <param name="options"></param>
  20. /// <param name="rectangle"></param>
  21. /// <returns></returns>
  22. public static void DrawOneDImageOnGraphics(string value, Rectangle rectangle, OneDDrawingOptions options)
  23. {
  24. OneDData data = OneDHelper.Encode(value, rectangle.Width);
  25. using (Bitmap dataImage = new Bitmap(data.Size, rectangle.Height, options.DrawGraphics))
  26. {
  27. Rectangle imageRect = new Rectangle(0, 0, dataImage.Width, dataImage.Height);
  28. BitmapData bmpData = dataImage.LockBits(imageRect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
  29. try
  30. {
  31. byte[] pixels = new byte[bmpData.Stride * bmpData.Height];
  32. int index = 0;
  33. for (int x = 0; x < bmpData.Width; x++)
  34. {
  35. Color color = (data[x] ? options.ForeColor : options.BackColor);
  36. pixels[index++] = color.B;
  37. pixels[index++] = color.G;
  38. pixels[index++] = color.R;
  39. pixels[index++] = color.A;
  40. }
  41. //index += padding;
  42. for (int y = 1; y < bmpData.Height; y++)
  43. {
  44. Array.Copy(pixels, 0, pixels, y * bmpData.Stride, bmpData.Stride);
  45. }
  46. Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
  47. }
  48. finally
  49. {
  50. dataImage.UnlockBits(bmpData);
  51. }
  52. Rectangle rect = new Rectangle(rectangle.Left + (rectangle.Width - dataImage.Width) / 2,
  53. rectangle.Top,
  54. dataImage.Width,
  55. dataImage.Height);
  56. //options.DrawGraphics.DrawImage(dataImage, rect, imageRect, GraphicsUnit.Pixel);
  57. options.DrawGraphics.DrawImage(dataImage, rect.Left, rect.Top, imageRect, GraphicsUnit.Pixel);
  58. }
  59. }
  60. /// <summary>
  61. /// 绘制图片
  62. /// </summary>
  63. /// <param name="data"></param>
  64. /// <param name="options"></param>
  65. /// <param name="rectangle"></param>
  66. /// <returns></returns>
  67. public static void DrawQRCodeImageOnGraphics(string value, Rectangle rectangle, QRCodeDrawingOptions options)
  68. {
  69. QRCodeData data = QRCodeHelper.Encode(value, rectangle.Width, rectangle.Height);
  70. using (Bitmap dataImage = new Bitmap(data.Size, data.Size, options.DrawGraphics))
  71. {
  72. Rectangle imageRect = new Rectangle(0, 0, dataImage.Width, dataImage.Height);
  73. BitmapData bmpData = dataImage.LockBits(imageRect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
  74. try
  75. {
  76. byte[] pixels = new byte[bmpData.Stride * bmpData.Height];
  77. int index = 0;
  78. for (int y = 0; y < data.Size; y++)
  79. {
  80. for (int x = 0; x < data.Size; x++)
  81. {
  82. Color color = (data[x, y] ? options.ForeColor : options.BackColor);
  83. pixels[index++] = color.B;
  84. pixels[index++] = color.G;
  85. pixels[index++] = color.R;
  86. pixels[index++] = color.A;
  87. }
  88. }
  89. Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
  90. }
  91. finally
  92. {
  93. dataImage.UnlockBits(bmpData);
  94. }
  95. Rectangle rect = new Rectangle(rectangle.Left + (rectangle.Width - dataImage.Width) / 2,
  96. rectangle.Top + (rectangle.Height - dataImage.Height) / 2,
  97. dataImage.Width,
  98. dataImage.Height);
  99. //options.DrawGraphics.DrawImage(dataImage, rect, imageRect, GraphicsUnit.Pixel);
  100. options.DrawGraphics.DrawImage(dataImage, rect.Left, rect.Top, imageRect, GraphicsUnit.Pixel);
  101. }
  102. }
  103. }
  104. }