<p><span style="color:#333333;font-family:Verdana, Arial, Helvetica, sans-serif"><span style="font-size: 13.3333px; background-color: rgb(255, 255, 255);"><img src="/Attach/201908/03112718-20190803111058.gif"/></span></span></p><p><span style="color:#333333;font-family:Verdana, Arial, Helvetica, sans-serif"><span style="font-size: 13.3333px; background-color: rgb(255, 255, 255);"> 代码来源:<a href="https://github.com/codebude/QRCoder">https://github.com/codebude/QRCoder</a> 的demo</span></span></p><p><span style="color:#333333;font-family:Verdana, Arial, Helvetica, sans-serif"><br/></span></p><pre class="brush:c#;toolbar:false"> private void RenderQrCode()
{
string level = comboBoxECC.SelectedItem.ToString();
QRCodeGenerator.ECCLevel eccLevel =
(QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel))
{
using (QRCode qrCode = new QRCode(qrCodeData))
{
pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, Color.Black, Color.White,
GetIconBitmap(), (int) iconSize.Value);
this.pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width,
pictureBoxQRCode.Height);
//Set the SizeMode to center the image.
this.pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
}
}
private Bitmap GetIconBitmap()
{
Bitmap img = null;
if (iconPath.Text.Length > 0)
{
try
{
img = new Bitmap(iconPath.Text);
}
catch (Exception)
{
}
}
return img;
}</pre><p><br/></p><p>附件附上图片及编译好的汉化过得qrcoder.dll<br/></p><p>qrcoder.dll 目前支持.netframework 3.5以上</p><pre class="brush:c#;toolbar:false">/// <summary>
/// 生成彩色二维码 /// </summary>
/// <param name="data">二维码内容</param>
/// <param name="filename">生成的图片名称(如:123.jpg)</param>
/// <param name="filepath">图片存放路径(如:Images\\CustomerQRCode)</param>
/// <returns></returns>
public static bool CreateQRCode(string data, string filename, string filepath)
{ if (string.IsNullOrWhiteSpace(data) || string.IsNullOrWhiteSpace(filename) || string.IsNullOrWhiteSpace(filepath)) return false;
QRCodeWriter writer = new QRCodeWriter();
Hashtable hints = new Hashtable();
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.Add(EncodeHintType.VERSION_START, 5);
Bitmap image = writer.encode(data, BarcodeFormat.QR_CODE, 0x200, 0x200, hints).ToBitmap();//黑白二维码
Bitmap bitmap2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawImage(image, 0, 0);
image.Dispose();
Bitmap bitmap3 = QrCodeVertical(bitmap2.Width, bitmap2.Height);//彩色渐变二维码
Color color = Color.FromArgb(200, 224, 114, 1); int num = 140; try
{
num -= (Encoding.UTF8.GetBytes(data).Length - 20) / 2;
} catch (Exception)
{
} int num2 = num; int num3 = num2; for (int i = 0; i < bitmap2.Width; i++)
{ for (int j = 0; j < bitmap2.Height; j++)
{
Color color3;
Color pixel = bitmap2.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)) ? bitmap3.GetPixel(i, j) : pixel;
}
bitmap2.SetPixel(i, j, color3);
}
}
bitmap3.Dispose(); #region 添加二维码标题 string str2 = "";//标题一
if (str2.Length < 1)
{
str2 = "MyTest";
} float emSize = 32f;
emSize -= (str2.Length - 4) * 1.8f;
Font font = new Font("微软雅黑", emSize, FontStyle.Bold);
SizeF ef = graphics.MeasureString(str2, font); float num7 = (bitmap2.Width - ef.Width) / 2f;
Brush brush = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
Brush brush2 = new SolidBrush(Color.White); int y = 50;
graphics.FillRectangle(brush2, new Rectangle((int)num7, y, (int)ef.Width, (int)ef.Height));
graphics.DrawString(str2, font, brush, (float)((int)num7), (float)y);
Brush brush3 = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2)); int width = 140;
graphics.FillEllipse(brush2, (bitmap2.Width - width) / 2, (bitmap2.Height - width) / 2, width, width); int num10 = 0x80;
graphics.FillEllipse(brush3, (bitmap2.Width - num10) / 2, (bitmap2.Height - num10) / 2, num10, num10); int num11 = 110;
graphics.FillEllipse(brush2, (bitmap2.Width - num11) / 2, (bitmap2.Height - num11) / 2, num11, num11); #endregion
#region 添加二维码中间内容
str2 = "";//标题二
if (str2.Length < 1)
{
str2 = "Test";
} float num12 = 32f;
num12 -= (str2.Length - 3) * 3.5f;
Font font2 = new Font("Meiryo", num12, FontStyle.Bold);
ef = graphics.MeasureString(str2, font2); float x = ((bitmap2.Width - ef.Width) / 2f) + 2f; float num14 = ((bitmap2.Height - ef.Height) / 2f) + 8f;
graphics.DrawString(str2, font2, brush3, x, num14);
graphics.Dispose(); #endregion
filepath = System.Web.HttpContext.Current.Server.MapPath(@"~\"+ filepath) + "\\" + filename; //filepath = filepath + filename;
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
bitmap2.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
image.Dispose(); return true;
}</pre><p><br/></p>