c#產生包含白邊和圖片的二維碼

來源:互聯網
上載者:User

標籤:pix   str   fill   drawing   ever   fromfile   try   class   演算法   

原文章連結忘記了。。。

1.引用nuget包:Install-Package ThoughtWorks.QRCode

2.可選,如果需要中間帶入圖片,儲存在項目中,在CreateQRCode()方法中傳入該圖片的相對路徑

3.直接調用下面的CreateQRCode方法

using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Web;using ThoughtWorks.QRCode.Codec;namespace Common{    public class QRCodeHelper    {        /// <summary>        ///  產生二維碼        /// </summary>        /// <param name="url">掃描後跳轉url</param>        /// <param name="insertImgUrl">中間圖片url(相對路徑),為空白則不帶圖片</param>        /// <returns></returns>        public static Bitmap CreateQRCode(string url,string insertImgUrl=null)        {            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;            qrCodeEncoder.QRCodeScale = 4;            qrCodeEncoder.QRCodeVersion = 8;            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;            Image image = qrCodeEncoder.Encode(url);            if (string.IsNullOrEmpty(insertImgUrl))            {                return AddFrame(image);            }            //插入圖片路徑            string path = HttpContext.Current.Server.MapPath(insertImgUrl);            var bitmap = AddFrame(CombinImage(image, path), 5);            return bitmap;        }        /// <summary>        /// 在圖片四周加入白邊        /// </summary>        /// <param name="Img">圖片</param>        /// <param name="Margin">白邊的高度,單位是像素</param>        /// <returns>Bitmap</returns>        private static Bitmap AddFrame(Image Img, int Margin=6)        {            //位元影像寬高            int width = Img.Width + Margin;            int height = Img.Height + Margin;            Bitmap BitmapResult = new Bitmap(width, height);            Graphics Grp = Graphics.FromImage(BitmapResult);            SolidBrush b = new SolidBrush(Color.White);//這裡修改顏色            Grp.FillRectangle(b, 0, 0, width, height);            System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, Img.Width, Img.Height);            //向矩形框內填充Img            Grp.DrawImage(Img, Margin/2, Margin/2 , Rec, GraphicsUnit.Pixel);            //返回位元影像檔案            Grp.Dispose();            GC.Collect();            return BitmapResult;        }        /// <summary>            /// 調用此函數後使此兩種圖片合并,類似相簿,有個            /// 背景圖,中間貼自己的靶心圖表片            /// </summary>            /// <param name="imgBack">粘貼的源圖片</param>            /// <param name="destImg">粘貼的靶心圖表片</param>            private static Image CombinImage(Image imgBack, string destImg)        {            Image img = Image.FromFile(destImg);        //照片圖片                  if (img.Height != 65 || img.Width != 65)            {                img = KiResizeImage(img, 65, 65, 0);            }            Graphics g = Graphics.FromImage(imgBack);            g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);      //g.DrawImage(imgBack, 0, 0, 相框寬, 相框高);                 //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一層黑色邊框                //g.DrawImage(img, 照片與相框的左邊距, 照片與相框的上邊距, 照片寬, 照片高);                g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);            GC.Collect();            return imgBack;        }        /// <summary>            /// Resize圖片            /// </summary>            /// <param name="bmp">原始Bitmap</param>            /// <param name="newW">新的寬度</param>            /// <param name="newH">新的高度</param>            /// <param name="Mode">保留著,暫時未用</param>            /// <returns>處理以後的圖片</returns>            private static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)        {            try            {                Image b = new Bitmap(newW, newH);                Graphics g = Graphics.FromImage(b);                // 插值演算法的品質                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);                g.Dispose();                return b;            }            catch            {                return null;            }        }    }}

  

c#產生包含白邊和圖片的二維碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.