C # generates two-dimensional code with white edges and pictures

Source: Internet
Author: User

The original article link forgot ...

1. Reference NuGet Package: Install-package Thoughtworks.qrcode

2. Optional, if you need to bring the picture in the middle, save in the project, in the Createqrcode () method to pass in the relative path of the picture

3. Call the following Createqrcode method directly

Using system;using system.drawing;using system.drawing.drawing2d;using system.web;using ThoughtWorks.QRCode.Codec; Namespace common{public class Qrcodehelper {//<summary>//Generate QR Code///&LT;/SUMMARY&G        T <param name= "url" > after scan jump url</param>//<param name= "Insertimgurl" > Middle picture url (relative path), empty without picture </p aram>//<returns></returns> public static Bitmap Createqrcode (String url,string Insertimgur            L=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);  }//Insert Picture path          String path = HttpContext.Current.Server.MapPath (Insertimgurl);            var bitmap = Addframe (combinimage (image, Path), 5);        return bitmap; }///<summary>///Add white edge around the picture///</summary>//<param name= "Img" > Picture </pa        ram>//<param name= "margin" > white edge height in pixels </param>//<returns>Bitmap</returns> private static Bitmap Addframe (Image Img, int margin=6) {//bitmap width high 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);//Modify the color here Grp.fillrectangle (b, 0, 0, width, height);            System.Drawing.Rectangle Rec = new System.Drawing.Rectangle (0, 0, img.width, img.height); Populate the Rectangle box with the IMG grp.drawimage (IMG, MARGIN/2, margin/2, Rec, GraphicsUnit.Pixel);            Returns the bitmap file Grp.dispose (); Gc.            Collect ();        return bitmapresult; }////<summary>///Call this function to merge the two pictures, similar to the album, there is A///background map, the middle of their own target image///</summa ry>//<param name= "Imgback" > pasted source Picture </param>//<param name= "destimg" > pasted target picture & lt;/param> private static Image combinimage (Image imgback, string destimg) {image img = I Mage.        FromFile (DESTIMG); Photo Image if (img. Height! = 65 | | Img.            Width! =) {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, frame width, frame height); G.fillrectangle (System.Drawing.Brushes.White, IMGBACK.WIDTH/2-img. WIDTH/2-1, IMGBACK.WIDTH/2-img. WIDTH/2-1,1,1);//Photo aroundBrush a layer of black border//g.drawimage (img, photo with picture frame on the left margin, photo with frame top margin, photo width, photo high); 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 Pictures///</summary>//<param name= "BMP" &G t; original bitmap</param>//<param name= "NEWW" > New width </param>//<param name= "NEWH" &G t; new heights </param>///<param Name= "Mode" > reserved, temporarily unused </param>//<returns> processing later pictures &            lt;/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);                The quality g.interpolationmode of interpolation algorithm = 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 # generates two-dimensional code with white edges and pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.