Generate a QR code with embedded Images

Source: Internet
Author: User

On the blog, I saw some people asking how to implement a QR code with embedded images. So I am going to record it for your reference.

 

1. First, prepare an embedded image.

2, since the generation of the QR code, it must be what kind of content to generate the QR code, here I use http://www.baidu.com as a string to generate the QR code

        private string QcodeSource        {            get            {                return "http://www.baidu.com";            }        }

3. Let's see how to generate a QR code based on qcodesource. Here we return byte []. PS: GMA. qrcodenet. encoding. net35.dll is used to generate the QR code library. Click the link to download it.

        public static byte[] GetQrCodeBitmapImage(string qrcode)        {            try            {                if (string.IsNullOrWhiteSpace(qrcode)) return null;                // Get QrCode GraphicsRenderer                var qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);                var qrCode = qrEncoder.Encode(qrcode);                var renderer = new GraphicsRenderer(new FixedModuleSize(24, QuietZoneModules.Four), Brushes.Black, Brushes.White);                using (var stream = new MemoryStream())                {                    renderer.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, stream);                    stream.Seek(0, SeekOrigin.Begin);                    stream.Flush();                    return stream.ToArray();                }            }            catch (Exception)            {                return null;            }        }

4. Since the byte [] of the image is returned, we will splice the embedded image and the QR code image into an image, how to program embedded images byte [] is not described here.

/// <Summary> /// call this function to merge the two types of images, similar to the album, with a // background image, paste your target image in the middle /// </Summary> /// <Param name = "sourceimage"> pasted source image </param> /// <Param name =" destbitmap "> pasted target image </param> Public static bitmap combinimage (Bitmap sourceimage, bitmap destbitmap) {If (destbitmap. height! = 250 | destbitmap. Width! = 250) {destbitmap = kiresizeimage (destbitmap, 250,250, 0);} using (var g = graphics. fromimage (sourceimage) {// G. drawimage (IMG, left margin of the photo and photo frame, top margin of the photo and photo frame, photo width, and photo height); G. drawimage (sourceimage, 0, 0, sourceimage. width, sourceimage. height); // G. fillrectangle (system. drawing. brushes. white, imgback. width/2-IMG. width/2-1, imgback. width/2-IMG. width/2-, 1); // brush a black border around the photo G. drawimage (destbitmap, sourceimage. width/2-destbitmap. width/2, sourceimage. width/2-destbitmap. width/2, destbitmap. width, destbitmap. height); GC. collect (); Return sourceimage ;}}

5. below

 

Conclusion: The generation is complete. The GMA. qrcodenet. encoding. net35.dll library is mainly used. The code is relatively simple.

 

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.