生成二维码的内容

using QRCoder;

            // 生成二维码的内容
string strCode = this.txtQr.Text.Trim();
if (string.IsNullOrWhiteSpace(strCode))
{
MessageBox.Show("请输入值");
return;
} //var bitmap = QrCodeHelper.Create(cont);
//pictureBox1.Image = bitmap; QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
QRCode qrcode = new QRCode(qrCodeData); // qrcode.GetGraphic 方法可参考最下发“补充说明”
Bitmap qrCodeImage = qrcode.GetGraphic(, Color.Black, Color.White, null, , , false);
MemoryStream ms = new MemoryStream();
//qrCodeImage.Save((ms, ImageFormat.Jpeg); // 如果想保存图片 可使用
string strDirPath = System.AppDomain.CurrentDomain.BaseDirectory + "QrCoderImg";
if (!Directory.Exists(strDirPath))
{
Directory.CreateDirectory(strDirPath);
}
string filePath = strDirPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
qrCodeImage.Save(filePath);
pictureBox1.ImageLocation = filePath; //// 响应类型
//context.Response.ContentType = "image/Jpeg";
////输出字符流
//context.Response.BinaryWrite(ms.ToArray());

带logo

/// <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 = ;//设置编码测量度
qRCodeEncoder.QRCodeVersion = ;//设置编码版本
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;
}
}
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 = ;
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 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 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;
} /// <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;
}
#endregion
}

C#生成二维码的内容的更多相关文章

  1. ThoughtWorks.QRCode生成二维码

    首先引用需要的dll,此处使用的是ThoughtWorks.QRCode.dll,网上可以找到对应的,此处也有一份,点击下载 http://files.cnblogs.com/files/ives/T ...

  2. 谷歌制图服务(Google Chart)接口生成二维码

    Google公布了制图服务(Google Chart)的接口,这项服务用起来相当简单,只使用浏览器就可以用来为统计数据自动生成图片. 目前谷歌制图服务提供折线图.条状图.饼图.Venn图.散点图.二维 ...

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

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

  4. C# 生成二维码 QRCoder

    最近项目上有个需求,需要将某个文件的下载地址生成二维码,并展示到网页上. 目前网上生成二维码的方法有好几种,本文将介绍[QRCoder]生成二维码的方式 一.首先通过VS中的[NUGET]下载并引用Q ...

  5. php背景图片上生成二维码,二维码上带logo 代码示例 (原)

    依赖库文件 phpqrcode.php (下载地址://www.jb51.net/codes/189897.html :或者在官网下载:http://phpqrcode.sourceforge.net ...

  6. C#生成二维码(可保存二维码图片)

    https://www.cnblogs.com/wlays/p/7994393.html 1.NuGet中搜索QRCoder,安装这个插件. 2.创建一个一般处理程序,代码: public void ...

  7. 小程序canvas生成二维码图片踩的坑

    1:生成临时图片,保证画布被加载以及渲染(即本身不可以 hidden 或是 上级元素不可以 hidden 或是 wx:if 隐藏等) == > 建议:因为 canvas 的组件层级(z-inde ...

  8. Java生成二维码(Java程序都可以使用)

    工具类,链接:https://pan.baidu.com/s/18U399fTH5wBJPnL97pAekg 提取码:bmw7 注:里面的corejar包是使用的zxing的代码,我只是将其导出的ja ...

  9. 基于Asp.Net Core,利用ZXing来生成二维码的一般流程

    本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作.对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191 ...

随机推荐

  1. 【ARC077F】SS kmp+打表找规律

    Description ​ 如果某个串可以由两个一样的串前后连接得到,我们就称之为"偶串".比如说"xyzxyz"和"aaaaaa"是偶串, ...

  2. 安装 CentOs 系统 及 Python 及 Scrapy 框架

    1: 先安装Centos 系统: 为什么选择CentOs系统,而不选择Ubuntu ? 我在Ubuntu上尝试了三次安装 python 和 Scrapy ,结果都没成功,在运维老王的建议下 使用Cen ...

  3. 安装jdk1.8,编写环境变量

    好记性不如烂笔头!!!(我这是把jdk放在的/usr/local下,且命令为jdk1.8...复制的时候别搞错位置了) JAVA_HOME=/usr/local/jdk1./ JAVA_BIN=/us ...

  4. MAC office2016 安装及激活(试了一下,靠谱, 非常感谢原作者)

    转载地址:https://blog.csdn.net/jxq0816/article/details/77248462 非常感谢原作者. 一.安装包下载地址 http://officecdn.micr ...

  5. vue 阻止冒泡弹窗小案例( 知识点:@click.stop=''")

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Vuex 使用了 module 后的访问方法 ..

    如果 使用了  module 和 namespace state 数据:=>   this.$store.state.User.info  (user 是模块名字. info 是 state 里 ...

  7. POJ1064 Cable master 【二分找最大值】

    题目:题目太长了! https://vjudge.net/problem/POJ-1064 题意分析:给了你N根长度为小数形式的棍子,再给出了你需要分的棍子的数量K,但要求你这K根棍子的长度必须是一样 ...

  8. 洛谷 P4269 / loj 2041 [SHOI2015] 聚变反应炉 题解【贪心】【DP】

    树上游戏..二合一? 题目描述 曾经发明了零件组装机的发明家 SHTSC 又公开了他的新发明:聚变反应炉--一种可以产生大量清洁能量的神秘装置. 众所周知,利用核聚变产生的能量有两个难点:一是控制核聚 ...

  9. 112th LeetCode Weekly Contest Minimum Increment to Make Array Unique

    Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...

  10. jQuery常用的方法

    each() 以每一个匹配的元素作为上下文来执行一个函数. size()  jQuery 对象中元素的个数.