C#生成带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
 }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持我们!

(0)

相关推荐

  • C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)

    用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法: 1):通过QrCodeNet(Gma.QrCodeNet.Encoding.dll)来实现 1.1):首先通过VS2015的NuGet下载对应的第三方组件,如下图所示: 1.2):具体生成二维码方法如下 private void GenerateQRByQrCodeNet() { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); Q

  • C#利用QrCode.Net生成二维码(Qr码)的方法

    现在网上很多应用都是用二维码来分享网址或者其它的信息.尤其在移动领域,二维码更是有很大的应用场景.因为项目的需要,需要在网站中增加一个生成二维码分析网址的功能,在谷歌大幅度抽筋的情况下无奈使用百度.百度N多,找到一些项目,但是可用性不强.(有一个项目是用VS2005开发的,在2010中调试不开.)终于在codeplex上找到一个"神器",这个"神器"可以很方便的生成二维码,速度那是相当的快,并且可支持中文,遵从MIT协议. QrCode.Net是一个使用C#编写的用

  • .NET C#利用ZXing生成、识别二维码/条形码

    一.首先下载 ZXing.Net 地址是:http://zxingnet.codeplex.com/releases/view/117068 然后将对应版本 .dll 拖入项目中,再引用之. 主要是用 BarcodeWriter.BarcodeReader. 二.生成二维码 .NET 平台的代码始终要简单些. QrCodeEncodingOptions options = new QrCodeEncodingOptions(); options.CharacterSet = "UTF-8&quo

  • asp.net C#生成和解析二维码的实例代码

    类库文件我们在文件最后面下载 [ThoughtWorks.QRCode.dll 就是类库] 使用时需要增加: 复制代码 代码如下: using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtWorks.QRCode.Codec.Util; 主要源代码: 1.生成二维码 复制代码 代码如下: QRCodeEncoder qrCodeEncoder = new QRCodeEncoder()

  • C#开发微信 二维码鼠标滑动 图像显示隐藏效果(推荐)

    客户端微信在二维码状态下,鼠标滑过,会有一张手机的图片滑动滑出,从隐藏到显示,从显示到隐藏. 思路很简单:1.设置透明度:2.给个移动的位移 先看下做的效果 整体代码也不难,就是给Image控件设置动画效果. <Grid x:Name="grid_content" Background="WhiteSmoke" Grid.Row="1"> <Grid.Triggers> <EventTrigger RoutedEve

  • asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例

    本文实例讲述了asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码.分享给大家供大家参考,具体如下: <%@ WebHandler Language="C#" Class="GetQRCode" %> using System; using System.Web; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtW

  • c#二维码生成的代码分享

    复制代码 代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary>/// 调用外网API 生成二维码  周祥 2013年11月12日10:54:38/// </summary>public class Qr{    public Qr()    {        //        //TODO: 在此处添加构造函数逻辑        /

  • c#裁剪图片后使用zxing生成二维码示例分享

    复制代码 代码如下: /// <summary>/// 生成二维码/// </summary>/// <param name="fileName">生成二维码路径</param>/// <param name="url">生成的内容</param>/// <param name="width">二维码宽</param>/// <param nam

  • C#使用第三方组件生成二维码汇总

    用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法: 1.通过QrCodeNet(Gma.QrCodeNet.Encoding.dll)来实现 1.1):首先通过VS2015的NuGet下载对应的第三方组件,如下图所示: 1.2):具体生成二维码方法如下 private void GenerateQRByQrCodeNet() { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); Qr

  • C# zxing二维码写入的实例代码

    复制代码 代码如下: private void button1_Click(object sender, EventArgs e)        {            if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))            {                MessageBox.Show("请输入需要转换的信息!");                return;            } string con

  • C#生成二维码的方法

    本文实例讲述了C#生成二维码的方法.分享给大家供大家参考.具体实现方法如下: 首先引用ThoughtWorks.QRCode.dll 具体代码如下: 复制代码 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using ThoughtWorks.QRCode.Codec; namespace QRCodeUtil {

随机推荐