效果如下:

1、无LOGO的二维码:

2、含有LOGO的二维码:

一、下载QrCode程序集:

使用的程序集有:

下载地址:

http://zxingnet.codeplex.com/

二、QRCodeHelper 工具类:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.QrCode;
using ZXing.QrCode.Internal; namespace Test.One.Common
{
public class QRCodeHelper
{
/// <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 = , sH = ;
// 按比例缩放
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) / , (destHeight - sH) / , sW, sH), , , imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[];
quality[] = ;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[] = encoderParam;
imgSource.Dispose();
return outBmp;
} /// <summary>
/// 生成二维码
/// </summary>
/// <param name="content">需要生成二维码的内容</param>
/// <param name="size">二维码图片长宽大小</param>
/// <returns></returns>
public static Bitmap Create(string content, int size)
{
try
{
var options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = size,
Height = size,
Margin = ,
ErrorCorrection = ErrorCorrectionLevel.H
};
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
var bmp = writer.Write(content);
return bmp;
}
catch (Exception ex)
{
return null;
}
} #region 合并用户QR图片和用户头像 /// <summary>
/// 合并用户QR图片和用户头像
/// </summary>
/// <param name="qrImg">QR图片(二维码图片)</param>
/// <param name="headerImg">用户头像</param>
/// <param name="n">缩放比例</param>
/// <returns></returns>
public static Bitmap MergeQrImg(Bitmap qrImg, Bitmap headerImg, double n = 0.23)
{
int margin = ;
float dpix = qrImg.HorizontalResolution;
float dpiy = qrImg.VerticalResolution;
var _newWidth = ( * qrImg.Width - * margin) * 1.0f / ;
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(, , newImgWidth - , newImgWidth - );
using (GraphicsPath path = CreateRoundedRectanglePath(rect, ))
{
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(, , _headerImg.Width - , _headerImg.Width - );
using (GraphicsPath path1 = CreateRoundedRectanglePath(rect1, ))
{
g1.DrawPath(p1, path1);
TextureBrush brush = new TextureBrush(_headerImg);
g1.FillPath(brush, path1);
}
g1.Dispose();
PointF center = new PointF((newImgWidth - _headerImg.Width) / , (newImgWidth - _headerImg.Height) / );
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, , );
PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / , (qrImg.Height - headerBgImg.Height) / );
g2.DrawImage(headerBgImg, center2);
g2.Dispose();
return backgroudImg;
} #endregion #region 图形处理 /// <summary>
/// 创建圆角矩形
/// </summary>
/// <param name="rect">区域</param>
/// <param name="cornerRadius">圆角角度</param>
/// <returns></returns>
private static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
//下午重新整理下,圆角矩形
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * , rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y, cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height - cornerRadius * );
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y + rect.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.X + cornerRadius * , rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * , rect.X, rect.Y + cornerRadius * );
roundedRect.CloseFigure();
return roundedRect;
} /// <summary>
/// 图片按比例缩放
/// </summary>
private static 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(, , newImage.Width, newImage.Height), new System.Drawing.Rectangle(, , initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
newG.Dispose();
return newImage;
} #endregion
}
}

测试代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.One.Common; namespace Test.One.Test
{
class Program
{
static void Main(string[] args)
{
#region 测试一、生成中间没有LOGO的二维码
//需要生成二维码的内容
//string content = "https://www.baidu.com";
//Bitmap bitmap = QRCodeHelper.Create(content, 300);
//string filename = DateTime.Now.ToFileTime().ToString();
//string saveFileUrl = string.Format(@"E:\RQCode\{0}.png", filename);
//bitmap.Save(saveFileUrl, System.Drawing.Imaging.ImageFormat.Png);
//bitmap.Dispose(); //释放资源
#endregion #region 测试二、生成带有LOGO二维码 //string qrFileUrl = @"E:\RQCode\131272918918217301.png";
//string headerFileUrl = @"E:\RQCode\111.png";
//string filename = DateTime.Now.ToFileTime().ToString();
//string saveFileUrl = string.Format(@"E:\RQCode\{0}.png", filename);
//Bitmap qrCodeBitmap = new Bitmap(qrFileUrl);
//Bitmap headerBitmap = new Bitmap(headerFileUrl);
//Bitmap qrAndHeaderBitmap = QRCodeHelper.MergeQrImg(qrCodeBitmap, headerBitmap);
//qrAndHeaderBitmap.Save(saveFileUrl, System.Drawing.Imaging.ImageFormat.Png);
//qrAndHeaderBitmap.Dispose(); //释放资源
//qrCodeBitmap.Dispose();
//headerBitmap.Dispose();
#endregion #region 测试三、生成无LOGO和有LOGO的二维码
string content = "http://www.baidu.com";
Bitmap bitmap = QRCodeHelper.Create(content, );
string qrCodefilename = DateTime.Now.ToFileTime().ToString();
string qrCodeSaveFileUrl = string.Format(@"E:\RQCode\{0}.png", qrCodefilename);
bitmap.Save(qrCodeSaveFileUrl, System.Drawing.Imaging.ImageFormat.Png); string headerFileUrl = @"E:\RQCode\111.png";
string filename = DateTime.Now.ToFileTime().ToString();
string saveFileUrl = string.Format(@"E:\RQCode\{0}.png", filename); Bitmap headerBitmap = new Bitmap(headerFileUrl);
Bitmap qrAndHeaderBitmap = QRCodeHelper.MergeQrImg(bitmap, headerBitmap);
qrAndHeaderBitmap.Save(saveFileUrl, System.Drawing.Imaging.ImageFormat.Png);
qrAndHeaderBitmap.Dispose(); //释放资源
bitmap.Dispose();
headerBitmap.Dispose();
#endregion Console.ReadLine();
}
}
}

C# 生成中间含有LOGO的二维码的更多相关文章

  1. 随手记一次利用开源zxing生成带嵌入logo的二维码图片

    之前就在项目里面用过zxing生成二维码,最近另一个项目同样需要用到二维码,故重新在学了学利用zxing生成二维码 接下来先做准备工作了,因为我是用vs2013上开发的,故选择了.net4.5版本的z ...

  2. PHP生成带logo图像二维码的两种方法

    本文主要和大家分享PHP生成带logo图像二维码的两种方法,主要以文字和代码的形式和大家分享,希望能帮助到大家. 一.利用Google API生成二维码Google提供了较为完善的二维码生成接口,调用 ...

  3. Thinkphp3.2结合phpqrcode生成二维码(含Logo的二维码),附案例

    首先,下载phpqrcode,将其解压到项目ThinkPHP\Library\Vendor目录下.Index_index.html(模板可自行配置) <form action="{:U ...

  4. windows phone 生产含logo的二维码

    这几天了解二维码了解的比较多,不过就是没深入了解.google了一下生产含logo二维码的思路,就是把logo给画到生成的二维码上,还是因为二维码的纠错能力足够好啊,用Graphics对图片进行操作? ...

  5. Java实现带logo的二维码

    Java实现带logo的二维码 二维码应用到生活的各个方面,会用代码实现二维码,我想一定是一项加分的技能.好了,我们来一起实现一下吧. 我们实现的二维码是基于QR Code的标准的,QR Code是由 ...

  6. C#生成带logo的二维码

    带logo的二维码生成分为两步骤:首先根据输入的内容生成二维码图片,然后读取本地的logo图片,通过图片处理生成带logo的二维码. 生成的二维码效果如下: 下面直接贴出二维码生成类   QRCode ...

  7. .NET生成带Logo的二维码

    使用ThoughtWorks.QRCode生成,利用这个库来生成带Logo的二维码(就是中间嵌了一个图片的二维码),直接见代码: HttpContext context = HttpContext.C ...

  8. C# 生成二维码,彩色二维码,带有Logo的二维码及普通条形码

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  9. C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)

    1.使用ZXint.Net生成带logo的二维码 /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name ...

随机推荐

  1. pdf嵌入字体

    论文提交时,要求所有的字体都是嵌入的,为这个问题折腾了很久,发现了一个很好的答案,记一下: http://stackoverflow.com/questions/4231656/how-do-i-em ...

  2. linux 系统运维

    Linux 系统监控1.进程    查看所有进程     ps -ef     ps -ef |grep nginx 结束进程    # 结束进程号为5031    kill -g 5031 # 结束 ...

  3. windbg入门

    1.下载安装windbg Windows 10 调试工具 (WinDbg) 如果你仅需要 Windows 10 调试工具,而不需要 WDK 10 或 Visual Studio 2015,你可以将调试 ...

  4. 【PRML读书笔记-Chapter1-Introduction】1.5 Decision Theory

    初体验: 概率论为我们提供了一个衡量和控制不确定性的统一的框架,也就是说计算出了一大堆的概率.那么,如何根据这些计算出的概率得到较好的结果,就是决策论要做的事情. 一个例子: 文中举了一个例子: 给定 ...

  5. 游戏开发工具之纹理打包器-3.使用GDI+绘图

    上一次我们实现了把我们要的图片添加到CTreeCtrl控件里去,并显示图片的缩略图,现在开始我们要讲比较重要的部分--绘图区.为了实现能编辑图片的功能,绘图区应该具有如下功能. 1.  添加删除图片. ...

  6. Java -- 获取当前日期、当月月初日期、月末日期

    Learn From:http://blog.csdn.net/sunhuwh/article/details/39161323 public class CalendarTest { public ...

  7. 转 iOS Core Animation 动画 入门学习(一)基础

    iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...

  8. 基于 Markdown 的开源的 Node.js 知识库平台

    Raneto 是一个免费,开源的 Node.js 知识库平台,基于静态 Markdown 文件实现. Raneto 可以被称为静态网站生成器,因为它并不需要数据库支持.所有的内容都存储在 Markdo ...

  9. 妙味5:document.cookie 操作

    本地环境中测试需要用fireFox,其它几个浏览器不行,服务器都可以测出正确结果   cookie特点: 1. 如登陆信息存储,同一论坛打开多个页面不用重复登陆,就是通过cookie来存取实现: 2. ...

  10. wcf服务返回json

    private static void CreateErrorReply(OperationContext operationContext, string key, HttpStatusCode s ...