C#生成指定二维码

2021-07-15 12:12

阅读:469

标签:str   break   ati   esc   www   定位   create   asp   put   

//using ThoughtWorks.QRCode.Codec;  引用dll
/// /// 生成二维码 /// /// 内容文本 /// 二维码编码方式 /// 纠错码等级 /// 二维码版本号 0-40 /// 每个小方格的预设宽度(像素),正整数 /// 图片尺寸(像素),0表示不设置 /// 图片白边(像素),当size大于0时有效 /// public System.Drawing.Image CreateQRCode(string Content, QRCodeEncoder.ENCODE_MODE QRCodeEncodeMode, QRCodeEncoder.ERROR_CORRECTION QRCodeErrorCorrect, int QRCodeVersion, int QRCodeScale, int size, int border) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncodeMode; qrCodeEncoder.QRCodeErrorCorrect = QRCodeErrorCorrect; qrCodeEncoder.QRCodeScale = QRCodeScale; qrCodeEncoder.QRCodeVersion = QRCodeVersion; System.Drawing.Image image = qrCodeEncoder.Encode(Content); #region 根据设定的目标图片尺寸调整二维码QRCodeScale设置,并添加边框 if (size > 0) { //当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸 #region 当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸 while (image.Width size && qrCodeEncoder.QRCodeScale > 1) { qrCodeEncoder.QRCodeScale--; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; if (image.Width 0) { while (image.Width 1) { qrCodeEncoder.QRCodeScale--; System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content); image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; } } #endregion //当目标图片尺寸大于二维码尺寸时,将二维码绘制在目标尺寸白色画布的中心位置 if (image.Width

  在aspx页面调用和输出为图片流

string content = string.IsNullOrEmpty(Request.QueryString["txt"]) ? "http://www.lenashane.com/" : Request.QueryString["txt"];
int size = Convert.ToInt32(string.IsNullOrEmpty(Request.QueryString["size"]) ? "200" : Request.QueryString["size"]);
int border = Convert.ToInt32(string.IsNullOrEmpty(Request.QueryString["border"]) ? "10" : Request.QueryString["border"]);
  
System.Drawing.Image image = CreateQRCode(content,
    QRCodeEncoder.ENCODE_MODE.BYTE,
    QRCodeEncoder.ERROR_CORRECTION.M,
    0,
    5,
    size,
    border);
  
//将图片输出到页面
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Png";
HttpContext.Current.Response.OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length);
HttpContext.Current.Response.End();
  
ms.Close();
ms = null;
image.Dispose();
image = null;

  生成二维码方法,增加定位点着色参数(System.Drawing.Color),效果如下:

/// 
/// 生成二维码
/// 
/// 内容文本
/// 二维码编码方式
/// 纠错码等级
/// 二维码版本号 0-40
/// 每个小方格的预设宽度(像素),正整数
/// 图片尺寸(像素),0表示不设置
/// 图片白边(像素),当size大于0时有效
/// 
public System.Drawing.Image CreateQRCode(string Content, QRCodeEncoder.ENCODE_MODE QRCodeEncodeMode, QRCodeEncoder.ERROR_CORRECTION QRCodeErrorCorrect, int QRCodeVersion, int QRCodeScale, int size, int border, Color codeEyeColor)
{
    QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncodeMode;
    qrCodeEncoder.QRCodeErrorCorrect = QRCodeErrorCorrect;
    qrCodeEncoder.QRCodeScale = QRCodeScale;
    qrCodeEncoder.QRCodeVersion = QRCodeVersion;
      
    System.Drawing.Image image = qrCodeEncoder.Encode(Content);
      
    #region 根据设定的目标图片尺寸调整二维码QRCodeScale设置,并添加边框
    if (size > 0)
    {
        //当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸
        #region 当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸
        while (image.Width  size && qrCodeEncoder.QRCodeScale > 1)
        {
            qrCodeEncoder.QRCodeScale--;
            System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content);
            image = new System.Drawing.Bitmap(imageNew);
            imageNew.Dispose();
            imageNew = null;
            if (image.Width  0)
        {
            while (image.Width  1)
            {
                qrCodeEncoder.QRCodeScale--;
                System.Drawing.Image imageNew = qrCodeEncoder.Encode(Content);
                image = new System.Drawing.Bitmap(imageNew);
                imageNew.Dispose();
                imageNew = null;
            }
        }
        #endregion
          
        //已经确认二维码图像,为图像染色修饰
        if (true)
        {
            //定位点方块边长
            int beSize = qrCodeEncoder.QRCodeScale * 3;
              
            int bep1_l = qrCodeEncoder.QRCodeScale * 2;
            int bep1_t = qrCodeEncoder.QRCodeScale * 2;
              
            int bep2_l = image.Width - qrCodeEncoder.QRCodeScale * 5 - 1;
            int bep2_t = qrCodeEncoder.QRCodeScale * 2;
              
            int bep3_l = qrCodeEncoder.QRCodeScale * 2;
            int bep3_t = image.Height - qrCodeEncoder.QRCodeScale * 5 - 1;
              
            int bep4_l = image.Width - qrCodeEncoder.QRCodeScale * 7 - 1;
            int bep4_t = image.Height - qrCodeEncoder.QRCodeScale * 7 - 1;
              
            System.Drawing.Graphics graphic0 = System.Drawing.Graphics.FromImage(image);
              
            // Create solid brush.
            SolidBrush blueBrush = new SolidBrush(codeEyeColor);
              
            // Fill rectangle to screen.
            graphic0.FillRectangle(blueBrush, bep1_l, bep1_t, beSize, beSize);
            graphic0.FillRectangle(blueBrush, bep2_l, bep2_t, beSize, beSize);
            graphic0.FillRectangle(blueBrush, bep3_l, bep3_t, beSize, beSize);
            graphic0.FillRectangle(blueBrush, bep4_l, bep4_t, qrCodeEncoder.QRCodeScale, qrCodeEncoder.QRCodeScale);
        }
          
        //当目标图片尺寸大于二维码尺寸时,将二维码绘制在目标尺寸白色画布的中心位置
        #region 如果目标尺寸大于生成的图片尺寸,将二维码绘制在目标尺寸白色画布的中心位置
        if (image.Width 

  

C#生成指定二维码

标签:str   break   ati   esc   www   定位   create   asp   put   

原文地址:https://www.cnblogs.com/bysx/p/9517688.html


评论


亲,登录后才可以留言!