1、點選項目名稱右鍵-->管理NuGet程式包,打開NuGet包管理器視窗,進行搜尋下載下傳即可。
2、生成二維碼:
public Bitmap CreateQRCode(string str, int width, int height)
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8", //編碼
Width = width, //寬度
Height = height //高度
};
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
Bitmap map = writer.Write(str);
return map;
}
普通二維碼效果圖
3、生成帶Logo二維碼
static Bitmap GenerateLogo(string text)
{
//Logo 圖檔
Bitmap logo = new Bitmap(@"D:\pic\34.jpg");
//構造二維碼寫碼器
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//生成二維碼
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
//擷取二維碼實際尺寸(去掉二維碼兩邊空白後的實際尺寸)
int[] rectangle = bm.getEnclosingRectangle();
//計算插入圖檔的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
int middleL = (map.Width - middleW) / 2;
int middleT = (map.Height - middleH) / 2;
//将img轉換成bmp格式,否則後面無法建立Graphics對象
Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmpimg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(map, 0, 0);
}
//将二維碼插入圖檔
Graphics myGraphic = Graphics.FromImage(bmpimg);
//白底
myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
//儲存成圖檔
return bmpimg;
}
帶Logo二維碼效果
4、生成彩色帶Logo的二維碼
public Bitmap GenerateColorLogo(string text)
{
//Logo 圖檔
Bitmap logo = new Bitmap(@"D:\pic\34.jpg");
//構造二維碼寫碼器
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//生成二維碼
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
//擷取二維碼實際尺寸(去掉二維碼兩邊空白後的實際尺寸)
int[] rectangle = bm.getEnclosingRectangle();
//計算插入圖檔的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
int middleL = (map.Width - middleW) / 2;
int middleT = (map.Height - middleH) / 2;
Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmpimg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(map, 0, 0);
}
//将二維碼插入圖檔
Graphics myGraphic = Graphics.FromImage(bmpimg);
//白底
myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
Bitmap map2 = new Bitmap(bmpimg.Width, bmpimg.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(map2);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawImage(bmpimg, 0, 0);
map.Dispose();
Bitmap map3 = QrCodeVertical(map2.Width, map2.Height);
Color color = Color.FromArgb(200, 233, 0, 1);
int num = 92;
try
{
num -= (Encoding.UTF8.GetBytes(text).Length - 20) / 2;
}
catch (Exception e)
{
Console.WriteLine(e);
}
int num2 = num;
int num3 = num2;
for (int i = 0; i < map2.Width; i++)
{
for (int j = 0; j < map2.Height; j++)
{
Color color3;
Color pixel = map2.GetPixel(i, j);
if ((i < num2) && (j < num3))
{
color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? color : pixel;
}
else
{
color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? map3.GetPixel(i, j) : pixel;
}
map2.SetPixel(i, j, color3);
}
}
map3.Dispose();
return map2;
}
///漸變顔色
private Bitmap QrCodeVertical(int width, int heigth)
{
var image = new Bitmap(width, heigth, PixelFormat.Format32bppArgb);
var rect = new Rectangle(0, 0, width, heigth);
var brush = new LinearGradientBrush(rect, Color.FromArgb(230, 0x01, 0xa9, 160), Color.FromArgb(0xff, 150, 90, 0x0), LinearGradientMode.Vertical);
Graphics graphics = Graphics.FromImage(image);
graphics.FillRectangle(brush, rect);
graphics.Dispose();
return image;
}
帶Logo的彩色二維碼