欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#生成帶logo的二維碼

 更新時(shí)間:2017年02月05日 10:27:42   作者:諸葛風(fēng)流  
帶logo的二維碼生成分為兩步驟:首先根據(jù)輸入的內(nèi)容生成二維碼圖片,然后讀取本地的logo圖片,通過圖片處理生成帶logo的二維碼。本文對(duì)此進(jìn)行介紹,具有很好的參考價(jià)值,下面跟著小編一起來看下吧

帶logo的二維碼生成分為兩步驟:首先根據(jù)輸入的內(nèi)容生成二維碼圖片,然后讀取本地的logo圖片,通過圖片處理生成帶logo的二維碼。

生成的二維碼效果如下:

下面直接貼出二維碼生成類   QRCodeHelper.cs  ,直接調(diào)用  CreateQRCodeWithLogo 方法,傳入相應(yīng)參數(shù)返回bitmap類型的數(shù)據(jù),直接將返回的數(shù)據(jù)綁定到圖片控件,如果是web可以先將圖片保存到服務(wù)器指定地址在獲取顯示

/// <summary>
 /// 生成帶logo二維碼
 /// </summary>
 public class QRCodeHelper
 {/// <summary>
  /// 創(chuàng)建二維碼
  /// </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;//設(shè)置二維碼編碼格式 
    qRCodeEncoder.QRCodeScale = 4;//設(shè)置編碼測量度    
    qRCodeEncoder.QRCodeVersion = 7;//設(shè)置編碼版本 
    qRCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//設(shè)置錯(cuò)誤校驗(yàn) 
    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;
  }
 }

下面是從網(wǎng)上找的圖片處理類   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>
  /// 創(chuàng)建圓角矩形
  /// </summary>
  /// <param name="rect">區(qū)域</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)
  {
   //縮略圖寬、高計(jì)算
   double newWidth = initImage.Width;
   double newHeight = initImage.Height;
   newWidth = n * initImage.Width;
   newHeight = n * initImage.Height;
   //生成新圖
   //新建一個(gè)bmp圖片
   System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
   //新建一個(gè)畫板
   System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
   //設(shè)置質(zhì)量
   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>
  /// 創(chuàng)建縮略圖
  /// </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);
   // 設(shè)置畫布的描繪質(zhì)量 
   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();
   // 以下代碼為保存圖片時(shí),設(shè)置壓縮質(zhì)量 
   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
 }

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • 詳解C#方法中使用out參數(shù)做登錄判斷代碼實(shí)例

    詳解C#方法中使用out參數(shù)做登錄判斷代碼實(shí)例

    這篇文章主要介紹了C#使用out參數(shù)做登錄判斷,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • C#逐行讀取txt文件的方法

    C#逐行讀取txt文件的方法

    這篇文章主要介紹了C#逐行讀取txt文件的方法,是C#程序設(shè)計(jì)中非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下
    2014-09-09
  • 基于Silverlight DataGrid中無代碼設(shè)置開始與結(jié)束日期DatePicker的實(shí)現(xiàn)方法

    基于Silverlight DataGrid中無代碼設(shè)置開始與結(jié)束日期DatePicker的實(shí)現(xiàn)方法

    本篇文章是對(duì)Silverlight DataGrid中無代碼設(shè)置開始與結(jié)束日期DatePicker的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#調(diào)用OutLokk實(shí)現(xiàn)發(fā)送郵件

    C#調(diào)用OutLokk實(shí)現(xiàn)發(fā)送郵件

    這篇文章主要為大家詳細(xì)介紹了如何利用C#調(diào)用OutLokk實(shí)現(xiàn)發(fā)送郵件的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • 詳解c# 可空類型(Nullable)

    詳解c# 可空類型(Nullable)

    這篇文章主要介紹了c# 可空類型(Nullable)的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#值類型和引用類型的深入理解

    C#值類型和引用類型的深入理解

    本篇文章主要是對(duì)C#中值類型和引用類型進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2014-01-01
  • DevExpress TreeList 常見問題解決方法

    DevExpress TreeList 常見問題解決方法

    這篇文章主要介紹了DevExpress TreeList 常見問題解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • WPF實(shí)現(xiàn)輪播圖效果(圖片、視屏)

    WPF實(shí)現(xiàn)輪播圖效果(圖片、視屏)

    這篇文章主要介紹了WPF實(shí)現(xiàn)輪播圖效果,以下是一個(gè)使用WPF技術(shù)實(shí)現(xiàn)圖片和視屏輪播的簡單案例代碼示例,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的小伙伴可以自己動(dòng)手試試
    2023-10-10
  • c#學(xué)習(xí)之30分鐘學(xué)會(huì)XAML

    c#學(xué)習(xí)之30分鐘學(xué)會(huì)XAML

    一個(gè)界面程序的核心,無疑就是界面和后臺(tái)代碼,而xaml就是微軟為構(gòu)建應(yīng)用程序界面而創(chuàng)建的一種描述性語言,也就是說,這東西是搞界面的
    2016-11-11
  • C#操作注冊(cè)表的方法

    C#操作注冊(cè)表的方法

    這篇文章介紹了C#操作注冊(cè)表的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論