| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- //using Curtain.Framework.Barcode;
- //using Curtain.Framework.Barcode.OneD;
- //using Curtain.Framework.Barcode.QRCode;
- using Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode;
- using Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode.OneD;
- using Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode.QRCode;
- namespace Dongke.WinForm.Controls.InvoiceLayout
- {
- public static class BarcodeDraw
- {
- /// <summary>
- /// 绘制图片
- /// </summary>
- /// <param name="data"></param>
- /// <param name="options"></param>
- /// <param name="rectangle"></param>
- /// <returns></returns>
- public static void DrawOneDImageOnGraphics(string value, Rectangle rectangle, OneDDrawingOptions options)
- {
- OneDData data = OneDHelper.Encode(value, rectangle.Width);
- using (Bitmap dataImage = new Bitmap(data.Size, rectangle.Height, options.DrawGraphics))
- {
- Rectangle imageRect = new Rectangle(0, 0, dataImage.Width, dataImage.Height);
- BitmapData bmpData = dataImage.LockBits(imageRect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
- try
- {
- byte[] pixels = new byte[bmpData.Stride * bmpData.Height];
- int index = 0;
- for (int x = 0; x < bmpData.Width; x++)
- {
- Color color = (data[x] ? options.ForeColor : options.BackColor);
- pixels[index++] = color.B;
- pixels[index++] = color.G;
- pixels[index++] = color.R;
- pixels[index++] = color.A;
- }
- //index += padding;
- for (int y = 1; y < bmpData.Height; y++)
- {
- Array.Copy(pixels, 0, pixels, y * bmpData.Stride, bmpData.Stride);
- }
- Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
- }
- finally
- {
- dataImage.UnlockBits(bmpData);
- }
- Rectangle rect = new Rectangle(rectangle.Left + (rectangle.Width - dataImage.Width) / 2,
- rectangle.Top,
- dataImage.Width,
- dataImage.Height);
- //options.DrawGraphics.DrawImage(dataImage, rect, imageRect, GraphicsUnit.Pixel);
- options.DrawGraphics.DrawImage(dataImage, rect.Left, rect.Top, imageRect, GraphicsUnit.Pixel);
- }
- }
- /// <summary>
- /// 绘制图片
- /// </summary>
- /// <param name="data"></param>
- /// <param name="options"></param>
- /// <param name="rectangle"></param>
- /// <returns></returns>
- public static void DrawQRCodeImageOnGraphics(string value, Rectangle rectangle, QRCodeDrawingOptions options)
- {
- QRCodeData data = QRCodeHelper.Encode(value, rectangle.Width, rectangle.Height);
- using (Bitmap dataImage = new Bitmap(data.Size, data.Size, options.DrawGraphics))
- {
- Rectangle imageRect = new Rectangle(0, 0, dataImage.Width, dataImage.Height);
- BitmapData bmpData = dataImage.LockBits(imageRect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
- try
- {
- byte[] pixels = new byte[bmpData.Stride * bmpData.Height];
- int index = 0;
- for (int y = 0; y < data.Size; y++)
- {
- for (int x = 0; x < data.Size; x++)
- {
- Color color = (data[x, y] ? options.ForeColor : options.BackColor);
- pixels[index++] = color.B;
- pixels[index++] = color.G;
- pixels[index++] = color.R;
- pixels[index++] = color.A;
- }
- }
- Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
- }
- finally
- {
- dataImage.UnlockBits(bmpData);
- }
- Rectangle rect = new Rectangle(rectangle.Left + (rectangle.Width - dataImage.Width) / 2,
- rectangle.Top + (rectangle.Height - dataImage.Height) / 2,
- dataImage.Width,
- dataImage.Height);
- //options.DrawGraphics.DrawImage(dataImage, rect, imageRect, GraphicsUnit.Pixel);
- options.DrawGraphics.DrawImage(dataImage, rect.Left, rect.Top, imageRect, GraphicsUnit.Pixel);
- }
- }
- }
- }
|