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;
namespace Dongke.WinForm.Controls.InvoiceLayout
{
public static class BarcodeDraw
{
///
/// 绘制图片
///
///
///
///
///
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);
}
}
///
/// 绘制图片
///
///
///
///
///
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);
}
}
}
}