带logo的二维码生成分为两步骤:首先根据输入的内容生成二维码图片,然后读取本地的logo图片,通过图片处理生成带logo的二维码。本文对此进行介绍,具有很好的参考价值,下面跟着小编一起来看下吧
带logo的二维码生成分为两步骤:首先根据输入的内容生成二维码图片,然后读取本地的logo图片,通过图片处理生成带logo的二维码。
生成的二维码效果如下:
下面直接贴出二维码生成类 qrcodehelper.cs ,直接调用 createqrcodewithlogo 方法,传入相应参数返回bitmap类型的数据,直接将返回的数据绑定到图片控件,如果是web可以先将图片保存到服务器指定地址在获取显示
/// <summary>
/// 生成带logo二维码
/// </summary>
public class qrcodehelper
{/// <summary>
/// 创建二维码
/// </summary>
/// <param name="content"></param>
/// <param name="size"></param>
/// <returns></returns>
public static bitmap create(string content)
{
try
{
//var options = new qrcodeencodingoptions
//{
// disableeci = true,
// characterset = "utf-8",
// width = size,
// height = size,
// margin = 0,
// errorcorrection = errorcorrectionlevel.h
//};
//var writer = new barcodewriter();
//writer.format = barcodeformat.qr_code;
//writer.options = options;
//var bmp = writer.write(content);
//return bmp;
qrcodeencoder qrcodeencoder = new qrcodeencoder();
qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.byte;//设置二维码编码格式
qrcodeencoder.qrcodescale = 4;//设置编码测量度
qrcodeencoder.qrcodeversion = 7;//设置编码版本
qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.m;//设置错误校验
bitmap image = qrcodeencoder.encode(content);
return image;
}
catch (exception ex)
{
return null;
}
}
/// <summary>
/// 获取本地图片
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private static bitmap getlocallog(string filename)
{
bitmap newbmp = new bitmap(filename);
//bitmap bmp = new bitmap(newbmp);
return newbmp;
}
/// <summary>
/// 生成带logo二维码
/// </summary>
/// <returns></returns>
public static bitmap createqrcodewithlogo(string content, string logopath)
{
//生成二维码
bitmap qrcode = create(content);
//生成logo
bitmap logo = getlocallog(logopath);
imageutility util = new imageutility();
bitmap finalimage = util.mergeqrimg(qrcode, logo);
return finalimage;
}
}
下面是从网上找的图片处理类 imageutility.cs
public class imageutility
{
#region 合并用户qr图片和用户头像
/// <summary>
/// 合并用户qr图片和用户头像
/// </summary>
/// <param name="qrimg">qr图片</param>
/// <param name="headerimg">用户头像</param>
/// <param name="n"></param>
/// <returns></returns>
public bitmap mergeqrimg(bitmap qrimg, bitmap headerimg, double n = 0.23)
{
int margin = 10;
float dpix = qrimg.horizontalresolution;
float dpiy = qrimg.verticalresolution;
var _newwidth = (10 * qrimg.width - 36 * margin) * 1.0f / 36;
var _headerimg = zoompic(headerimg, _newwidth / headerimg.width);
//处理头像
int newimgwidth = _headerimg.width + margin;
bitmap headerbgimg = new bitmap(newimgwidth, newimgwidth);
headerbgimg.maketransparent();
graphics g = graphics.fromimage(headerbgimg);
g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
g.clear(color.transparent);
pen p = new pen(new solidbrush(color.white));
rectangle rect = new rectangle(0, 0, newimgwidth - 1, newimgwidth - 1);
using (graphicspath path = createroundedrectanglepath(rect, 1))
{
g.drawpath(p, path);
g.fillpath(new solidbrush(color.white), path);
}
//画头像
bitmap img1 = new bitmap(_headerimg.width, _headerimg.width);
graphics g1 = graphics.fromimage(img1);
g1.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
g1.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
g1.clear(color.transparent);
pen p1 = new pen(new solidbrush(color.gray));
rectangle rect1 = new rectangle(0, 0, _headerimg.width - 1, _headerimg.width - 1);
using (graphicspath path1 = createroundedrectanglepath(rect1, 1))
{
g1.drawpath(p1, path1);
texturebrush brush = new texturebrush(_headerimg);
g1.fillpath(brush, path1);
}
g1.dispose();
pointf center = new pointf((newimgwidth - _headerimg.width) / 2, (newimgwidth - _headerimg.height) / 2);
g.drawimage(img1, center.x, center.y, _headerimg.width, _headerimg.height);
g.dispose();
bitmap backgroudimg = new bitmap(qrimg.width, qrimg.height);
backgroudimg.maketransparent();
backgroudimg.setresolution(dpix, dpiy);
headerbgimg.setresolution(dpix, dpiy);
graphics g2 = graphics.fromimage(backgroudimg);
g2.clear(color.transparent);
g2.drawimage(qrimg, 0, 0);
pointf center2 = new pointf((qrimg.width - headerbgimg.width) / 2, (qrimg.height - headerbgimg.height) / 2);
g2.drawimage(headerbgimg, center2);
g2.dispose();
return backgroudimg;
}
#endregion
#region 图形处理
/// <summary>
/// 创建圆角矩形
/// </summary>
/// <param name="rect">区域</param>
/// <param name="cornerradius">圆角角度</param>
/// <returns></returns>
private graphicspath createroundedrectanglepath(rectangle rect, int cornerradius)
{
//下午重新整理下,圆角矩形
graphicspath roundedrect = new graphicspath();
roundedrect.addarc(rect.x, rect.y, cornerradius * 2, cornerradius * 2, 180, 90);
roundedrect.addline(rect.x + cornerradius, rect.y, rect.right - cornerradius * 2, rect.y);
roundedrect.addarc(rect.x + rect.width - cornerradius * 2, rect.y, cornerradius * 2, cornerradius * 2, 270, 90);
roundedrect.addline(rect.right, rect.y + cornerradius * 2, rect.right, rect.y + rect.height - cornerradius * 2);
roundedrect.addarc(rect.x + rect.width - cornerradius * 2, rect.y + rect.height - cornerradius * 2,
cornerradius * 2, cornerradius * 2, 0, 90);
roundedrect.addline(rect.right - cornerradius * 2, rect.bottom, rect.x + cornerradius * 2, rect.bottom);
roundedrect.addarc(rect.x, rect.bottom - cornerradius * 2, cornerradius * 2, cornerradius * 2, 90, 90);
roundedrect.addline(rect.x, rect.bottom - cornerradius * 2, rect.x, rect.y + cornerradius * 2);
roundedrect.closefigure();
return roundedrect;
}
/// <summary>
/// 图片按比例缩放
/// </summary>
private image zoompic(image initimage, double n)
{
//缩略图宽、高计算
double newwidth = initimage.width;
double newheight = initimage.height;
newwidth = n * initimage.width;
newheight = n * initimage.height;
//生成新图
//新建一个bmp图片
system.drawing.image newimage = new system.drawing.bitmap((int)newwidth, (int)newheight);
//新建一个画板
system.drawing.graphics newg = system.drawing.graphics.fromimage(newimage);
//设置质量
newg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
newg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//置背景色
newg.clear(color.transparent);
//画图
newg.drawimage(initimage, new system.drawing.rectangle(0, 0, newimage.width, newimage.height),
new system.drawing.rectangle(0, 0, initimage.width, initimage.height), system.drawing.graphicsunit.pixel);
newg.dispose();
return newimage;
}
/// <summary>
/// 创建缩略图
/// </summary>
/// <param name="b"></param>
/// <param name="destheight"></param>
/// <param name="destwidth"></param>
/// <returns></returns>
public static bitmap getthumbnail(bitmap b, int destheight, int destwidth)
{
system.drawing.image imgsource = b;
system.drawing.imaging.imageformat thisformat = imgsource.rawformat;
int sw = 0, sh = 0;
// 按比例缩放
int swidth = imgsource.width;
int sheight = imgsource.height;
if (sheight > destheight || swidth > destwidth)
{
if ((swidth * destheight) > (sheight * destwidth))
{
sw = destwidth;
sh = (destwidth * sheight) / swidth;
}
else
{
sh = destheight;
sw = (swidth * destheight) / sheight;
}
}
else
{
sw = swidth;
sh = sheight;
}
bitmap outbmp = new bitmap(destwidth, destheight);
graphics g = graphics.fromimage(outbmp);
g.clear(color.transparent);
// 设置画布的描绘质量
g.compositingquality = compositingquality.highquality;
g.smoothingmode = smoothingmode.highquality;
g.interpolationmode = interpolationmode.highqualitybicubic;
g.drawimage(imgsource, new rectangle((destwidth - sw) / 2, (destheight - sh) / 2, sw, sh), 0, 0,
imgsource.width, imgsource.height, graphicsunit.pixel);
g.dispose();
// 以下代码为保存图片时,设置压缩质量
encoderparameters encoderparams = new encoderparameters();
long[] quality = new long[1];
quality[0] = 100;
encoderparameter encoderparam = new encoderparameter(system.drawing.imaging.encoder.quality, quality);
encoderparams.param[0] = encoderparam;
imgsource.dispose();
return outbmp;
}
#endregion
}
以上就是使用c#生成带logo的二维码的示例代码分享的详细内容。