Sample code sharing using C # to generate a QR code with logo

Source: Internet
Author: User
Tags bmp image
The two-dimensional code with logo generation is divided into three steps: first, according to the input content generated two-dimensional code image, and then read the local logo image, through the image processing to generate a logo with the QR code. This article introduces this, has a very good reference value, follow the small series below to see it

The two-dimensional code with logo generation is divided into three steps: first, according to the input content generated two-dimensional code image, and then read the local logo image, through the image processing to generate a logo with the QR code.

The resulting QR code effect is as follows:

The following directly paste the two-dimensional code generation class QRCodeHelper.cs, directly call the Createqrcodewithlogo method, pass in the corresponding parameter return bitmap type of data, directly bind the returned data to the picture control, If the web can first save the picture to the server, specify the address in the Get display

<summary>//Generate QR code with logo////</summary> public class Qrcodehelper {///<summary>//Create two-dimensional code//&LT ;/summary>//<param name= "content" ></param>///<param name= "size" ></param>//<retur ns></returns> public static Bitmap Create (string content) {try {//var options = new Qrcodeencodingopti ONS//{//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;//Setting the QR code encoding format Qrcodeencoder.qrcodescale = 4;//Setting the encoding measurement qrcodeencoder.qrcodeversion = 7;//Setting the encoded version Qrcodeenc oder. Qrcodeerrorcorrect = Qrcodeencoder.error_coRrection.    m;//Set Error check Bitmap image = Qrcodeencoder.encode (content);   return image;   } catch (Exception ex) {return null; }}///<summary>//Get local Pictures///</summary>//<param name= "FileName" ></param>//<retu   rns></returns> private static Bitmap Getlocallog (string filename) {Bitmap newbmp = new Bitmap (fileName);   Bitmap bmp = new Bitmap (newbmp);  return newbmp; }///<summary>//Generate logo QR code////</summary>//<returns></returns> public static Bitmap Cr   Eateqrcodewithlogo (string content, String Logopath) {//Generate QR code Bitmap QRCode = Create (content);   Create logo Bitmap logo = Getlocallog (logopath);   imageutility util = new imageutility (); Bitmap finalimage = util.   Mergeqrimg (qrcode, logo);  return finalimage; } }

Below is the image processing class ImageUtility.cs

public class Imageutility {#region merge user QR images and user avatars////<summary>//merge user's QR images and user avatars///</summary>//< param name= "qrimg" &GT;QR pictures </param>///<param Name= "headerimg" > User picture </param>//<param name= "n" & gt;</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 = (Ten * qrimg.width-36 * margin) * 1.0F/36;   var _headerimg = Zoompic (headerimg, _newwidth/headerimg.width);   handle Avatar 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);   }//Portrait 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 Graphics Processing//<summary>//create rounded rectangles////</summary>//<param name= "rect" > Zone &LT;/PA ram>//<param name= "Cornerradius" > Fillet angle </param>//<returns></returns> Private  GraphicsPath Createroundedrectanglepath (Rectangle rect, int cornerradius) {//PM re-organize, rounded rectangle GraphicsPath roundedrect =   New GraphicsPath (); Roundedrect.addarc (rect. X, Rect. YCornerradius * 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>///Picture scaled////</summary> Private image zoompic (image initimage, double n) {//thumbnail width, high computational double newwidth = initimage.width;   Double newheight = initimage.height;   Newwidth = n * initimage.width;   Newheight = n * initimage.height;   Generate a new diagram//create a new BMP image System.Drawing.Image NewImage = new System.Drawing.Bitmap ((int) newwidth, (int) newheight);   Create a new artboard System.Drawing.Graphics NEWG = System.Drawing.Graphics.FromImage (newimage);   Set quality Newg.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;   Newg.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;   Background color newg.clear (color.transparent); Drawing Newg.drawimage (Initimage, New System.Drawing.Rectangle (0, 0, Newimage.width, newimage.height), new System.drawin   G.rectangle (0, 0, Initimage.width, initimage.height), System.Drawing.GraphicsUnit.Pixel);   Newg.dispose ();  return newimage; }///<summary>//Create thumbnails///</summary>//<param name= "B" ></param>//<param name= "des Theight "></param>//<param name=" DestwidTH "></param>///<returns></returns> public static Bitmap GetThumbnail (Bitmap B, Int. Destheight, in   T destwidth) {System.Drawing.Image imgsource = b;   System.Drawing.Imaging.ImageFormat Thisformat = Imgsource.rawformat;   int SW = 0, SH = 0;   proportionally scales 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);   Set the drawing quality of the canvas 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, GRAPHICSU Nit.   Pixel);   G.dispose ();   The following code sets the compression quality encoderparameters Encoderparams = new EncoderParameters () when saving the picture;   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}
Related Article

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.