使用C#產生帶logo的二維碼的範例程式碼分享

來源:互聯網
上載者:User
帶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 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.