引用 ZXing
然后如下代码
public static class BarcodeGenerator { public enum OutputFormat { PNG, SVG } public static String GeneratePng(string content, BarcodeFormat barcodeformat, int width, int height, int margin, string alt) { var qrWriter = new ZXing.BarcodeWriterPixelData { Format = barcodeformat, Options = new EncodingOptions { Height = height, Width = width, Margin = margin } }; var pixelData = qrWriter.Write(content); // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB // the System.Drawing.Bitmap class is provided by the CoreCompat.System.Drawing package using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)) using (var ms = new MemoryStream()) { // lock the data area for fast access var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); try { // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length); } finally { bitmap.UnlockBits(bitmapData); } // save to stream as PNG bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); //output.TagName = "img"; //output.Attributes.Clear(); //output.Attributes.Add("width", pixelData.Width); //output.Attributes.Add("height", pixelData.Height); //output.Attributes.Add("alt", alt); //output.Attributes.Add("src", // String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray()))); return String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())); } } public static String GenerateSvg(string content, BarcodeFormat barcodeformat, int width, int height, int margin, string alt) { var qrWriter = new ZXing.BarcodeWriterSvg { Format = barcodeformat, Options = new EncodingOptions { Height = height, Width = width, Margin = margin } }; var svgImage = qrWriter.Write(content); return String.Format("data:image/svg+xml;{0}", svgImage.Content); //output.TagName = "img"; //output.Attributes.Clear(); //output.Attributes.Add("width", svgImage.Width); //output.Attributes.Add("height", svgImage.Height); //output.Attributes.Add("alt", alt); //output.Attributes.Add("src", new Microsoft.AspNetCore.Html.HtmlString( // String.Format("data:image/svg+xml;{0}", svgImage.Content))); } }
public static class ImgConvert { #region base64转图片 /// <summary> /// 图片上传 Base64解码 /// </summary> /// <param name="dataURL">Base64数据</param> /// <param name="imgName">图片名字</param> /// <returns>返回一个相对路径</returns> public static string DecodeBase64ToImage(this string dataURL, string imgName) { string path = ""; String base64 = dataURL.Substring(dataURL.IndexOf(",") + 1); //将‘,’以前的多余字符串删除 System.Drawing.Bitmap bitmap = null;//定义一个Bitmap对象,接收转换完成的图片 try//会有异常抛出,try,catch一下 { byte[] arr = Convert.FromBase64String(base64);//将纯净资源Base64转换成等效的8位无符号整形数组 System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);//转换成无法调整大小的MemoryStream对象 bitmap = new System.Drawing.Bitmap(ms);//将MemoryStream对象转换成Bitmap对象 //string url = HttpRuntime.AppDomainAppPath.ToString(); //string tmpRootDir = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString()); //获取程序根目录 string tmpRootDir = AppContext.BaseDirectory; string imagesurl2 =Path.Combine(tmpRootDir, imgName + ".jpg"); path = imagesurl2; bitmap.Save(imagesurl2, System.Drawing.Imaging.ImageFormat.Jpeg);//保存到服务器路径 //bitmap.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); //bitmap.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif); //bitmap.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png); ms.Close();//关闭当前流,并释放所有与之关联的资源 bitmap.Dispose(); } catch (Exception e) { string massage = e.Message; } return path;//返回相对路径 } #endregion #region 图片转base64 /// <summary> /// 图片转base64 /// </summary> /// <param name="path">图片路径</param><br> /// <returns>返回一个base64字符串</returns> public static string DecodeImageToBase64(this string path) { if (!File.Exists(path)) { return ""; } string base64str = ""; try { //读图片转为Base64String System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(path); //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(path); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); bmp.Dispose(); base64str = Convert.ToBase64String(arr); } catch (Exception e) { string mss = e.Message; } return "data:image/jpg;base64," + base64str; } #endregion }
使用
string url="存入二维码的值"; string qrCode = BarcodeGenerator.GeneratePng(url, ZXing.BarcodeFormat.QR_CODE, 150, 150, 0, url); String base64 = qrCode.Substring(qrCode.IndexOf(",") + 1); byte[] arr = Convert.FromBase64String(base64); var filePath = qrCode.DecodeBase64ToImage("图片名称");