Today to do the project when there is a generation of two-dimensional code and in the middle of the need to join the logo, hands-on test a few, the total feeling effect did not write well before the old code, it is really familiar with the taste, generate a picture as follows
The left side is inside, and the right side is my own generation.
The principle is relatively simple, but the details are still somewhat complex, nonsense not much to say directly on the code
public class Qrcodehelper{public static Bitmap GetThumbnail (Bitmap b, int destheight, int destwidth) { System.Drawing.Image Imgsource = b; System.Drawing.Imaging.ImageFormat Thisformat = Imgsource.rawformat;int SW = 0, SH = 0;//proportionally scaled int swidth = Imgsource.wid Th;int sheight = imgsource.height;if (sheight > Destheight | | swidth > Destwidth) {if ((Swidth * destheight) > (SH Eight * 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 canvas's depiction quality 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, GraphicsUnit.Pixel); G.dispose ();//The following generationsTo save the image, set the compression quality encoderparameters Encoderparams = new EncoderParameters (); 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;} public static Bitmap Create (string content, int size) {Try{var options = new Qrcodeencodingoptions{disableeci = True,charac Terset = "UTF-8", Width = Size,height = Size,margin = 0,errorcorrection = Errorcorrectionlevel.h};var writer = new Barcodew Riter (); writer. Format = Barcodeformat.qr_code;writer. Options = Options;var bmp = writer. Write (content); return bmp;} catch (Exception ex) {return null;}}} public class imageutility{#region Merge user qr images and user avatars///<summary>///merge user QR images and user avatars///</summary>///<param Name= "qrimg" >qr pictures </param>///<param name= "headerimg" > User avatar </param>///<param name= "n" > Scale </param>///<returns></returns>public Bitmap mergeqrimg (bitmAP Qrimg, Bitmap headerimg, double n = 0.23) {int margin = 10;float Dpix = qrimg.horizontalresolution;float Dpiy = qrimg.ve Rticalresolution;var _newwidth = (Ten * qrimg.width-46 * margin) * 1.0f/46;var _headerimg = zoompic (headerimg, _newWidt H/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 = Createroundedrectanglepa Th (Rect, 7)) {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 = Createrounde Drectanglepath (Rect1, 7)) {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>///creating rounded rectangles///</summary>///<param name= "rect" > Area </param> <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. Y, Cornerradius * 2, Cornerradius * 2, 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, (+); 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, +); 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, N/A, and 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 calculation double Newwidth = initimage.width;double Newheight = Initimage.height;newwidth = n * initimage.width;newheight = n * initImage.He ight;//generate a new image//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.Drawing.Rectangle (0, 0, Initimage.width, Initimage.height), System.Drawing.GraphicsUnit.Pixel); Newg.dispose (); return newimage;} #endregion}
C # generates two-dimensional code and adds logo in the middle