QRCodeHelper 二维码生成
1 using System; 2 using System.Drawing; 3 using ThoughtWorks.QRCode.Codec; 4 using System.IO; 5 using System.Text; 6 using System.Drawing.Drawing2D; 7 using System.Web; 8 9 namespace APP.QrCodes 10 { 11 public class QRCodeHelper 12 { 13 public enum ImgEnum 14 { 15 JPG 16 } 17 18 public static string GetImgType(ImgEnum jpg) 19 { 20 string ext = string.Empty; 21 switch (jpg) 22 { 23 case ImgEnum.JPG: 24 ext = ".jpg"; 25 break; 26 default: 27 break; 28 } 29 return ext; 30 } 31 /// <summary> 32 /// 生成二维码图片 33 /// </summary> 34 /// <param name="codeNumber">要生成二维码的字符串</param> 35 /// <param name="size">大小尺寸</param> 36 /// <returns>二维码图片</returns> 37 public Bitmap Create_ImgCode(string codeNumber, int size) 38 { 39 //创建二维码生成类 40 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 41 //设置编码模式 42 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 43 //设置编码测量度 44 qrCodeEncoder.QRCodeScale = size; 45 //设置编码版本 46 qrCodeEncoder.QRCodeVersion = 0; 47 //设置编码错误纠正 48 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 49 //生成二维码图片 50 System.Drawing.Bitmap image = qrCodeEncoder.Encode(codeNumber, Encoding.UTF8); 51 52 return image; 53 } 54 55 public static string CreateLogQrCode(string strContent) 56 { 57 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 58 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 59 qrCodeEncoder.QRCodeScale = 5; 60 qrCodeEncoder.QRCodeVersion = 8; 61 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; 62 System.Drawing.Image image = qrCodeEncoder.Encode(strContent, Encoding.UTF8);//生成二维码图片 63 64 System.IO.MemoryStream MStream = new System.IO.MemoryStream(); 65 image.Save(MStream,System.Drawing.Imaging.ImageFormat.Png); 66 67 System.IO.MemoryStream MStream1 = new System.IO.MemoryStream(); 68 CombinImage(image, HttpRuntime.AppDomainAppPath + "QrCodes/logo.png").Save(MStream1, System.Drawing.Imaging.ImageFormat.Png); 69 70 string reStr = Convert.ToBase64String(MStream1.ToArray()); 71 //image.Dispose(); 72 MStream.Dispose(); 73 MStream1.Dispose(); 74 return reStr; 75 } 76 77 public string SaveImg(Bitmap bmp, string path) 78 { 79 bmp.Save(path); 80 return path; 81 } 82 83 /// <summary> 84 /// 保存图片 85 /// </summary> 86 /// <param name="bmp">源</param> 87 /// <param name="path">路径</param> 88 /// <param name="jpg">格式</param> 89 /// <returns></returns> 90 public bool SaveImg(Bitmap bmp, string path,ImgEnum jpg) 91 { 92 try 93 { 94 bmp.Save(path); 95 return true; 96 } 97 catch 98 { 99 return false; 100 } 101 } 102 103 /// <summary> 104 /// 保存图片 105 /// </summary> 106 /// <param name="bmp">源</param> 107 /// <param name="path">路径</param> 108 /// <param name="path">文件名</param> 109 /// <returns></returns> 110 public bool SaveImg(Bitmap bmp, string path,string fileName) 111 { 112 try 113 { 114 bmp.Save(path+fileName); 115 return true; 116 } 117 catch 118 { 119 return false; 120 } 121 } 122 123 /// <summary> 124 /// 保存图片 125 /// </summary> 126 /// <param name="bmp">源</param> 127 /// <param name="path">路径</param> 128 /// <param name="path">文件名</param> 129 /// <param name="jpg">格式</param> 130 /// <returns></returns> 131 public bool SaveImg(Bitmap bmp, string path, string fileName,ImgEnum jpg) 132 { 133 try 134 { 135 var ext = GetImgType(jpg); 136 bmp.Save(path + fileName + ext); 137 return true; 138 } 139 catch 140 { 141 return false; 142 } 143 } 144 145 /// <summary> 146 /// Bitmap转化base64编码 147 /// </summary> 148 /// <param name="bmp"></param> 149 /// <returns></returns> 150 public string GetBase64String(Bitmap bmp) 151 { 152 MemoryStream ms = new MemoryStream(); 153 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 154 byte[] arr = new byte[ms.Length]; 155 ms.Position = 0; 156 ms.Read(arr, 0, (int)ms.Length); 157 ms.Close(); 158 string pic = Convert.ToBase64String(arr); 159 return pic; 160 } 161 162 /// <summary> 163 /// 得到流 164 /// </summary> 165 /// <param name="bmp"></param> 166 /// <returns></returns> 167 public Stream GetStream(Bitmap bmp) 168 { 169 MemoryStream ms = new MemoryStream(); 170 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 171 byte[] arr = new byte[ms.Length]; 172 ms.Position = 0; 173 ms.Read(arr, 0, (int)ms.Length); 174 ms.Close(); 175 return ms; 176 } 177 178 179 /// <summary> 180 /// 调用此函数后使此两种图片合并,类似相册,有个 181 /// 背景图,中间贴自己的目标图片 182 /// </summary> 183 /// <param name="imgBack">粘贴的源图片</param> 184 /// <param name="destImg">粘贴的目标图片</param> 185 /// <summary> 186 public static Image CombinImage(Image imgBack, string destImg) 187 { 188 Image img = Image.FromFile(destImg); //照片图片 189 if (img.Height != 65 || img.Width != 65) 190 { 191 img = KiResizeImage(img, 65, 65, 0); 192 } 193 Graphics g = Graphics.FromImage(imgBack); 194 195 g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); 196 197 //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框 198 199 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); 200 201 g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height); 202 GC.Collect(); 203 return imgBack; 204 } 205 206 /// <summary> 207 /// Resize图片 208 /// </summary> 209 /// <param name="bmp">原始Bitmap</param> 210 /// <param name="newW">新的宽度</param> 211 /// <param name="newH">新的高度</param> 212 /// <param name="Mode">保留着,暂时未用</param> 213 /// <returns>处理以后的图片</returns> 214 public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode) 215 { 216 try 217 { 218 Image b = new Bitmap(newW, newH); 219 Graphics g = Graphics.FromImage(b); 220 // 插值算法的质量 221 g.InterpolationMode = InterpolationMode.HighQualityBicubic; 222 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); 223 g.Dispose(); 224 return b; 225 } 226 catch 227 { 228 return null; 229 } 230 } 231 } 232 }