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.