BarcodeDraw.cs 4.6 KB

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