/// <summary> /// base64编码的文本转为图片 /// </summary> /// <param name="txtFilePath">文件相对路径(存到服务器上)</param> /// <param name="str">图片字符串</param> private void Base64StringToImage(string txtFilePath, string str) { try { String inputStr = str; byte[] arr = Convert.FromBase64String(inputStr); MemoryStream ms = new MemoryStream(arr); Bitmap bmp = new Bitmap(ms); bmp.Save(System.Web.HttpContext.Current.Server.MapPath(txtFilePath) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp); //bmp.Save(txtFileName + ".gif", ImageFormat.Gif); //bmp.Save(txtFileName + ".png", ImageFormat.Png); ms.Close(); //imgPhoto.ImageUrl = txtFilePath + ".jpg"; //MessageBox.Show("转换成功!"); } catch (Exception ex) { } }
protected string ImgToBase64String(string ImagePath) { try { Bitmap bmp = new Bitmap(System.Web.HttpContext.Current.Server.MapPath(ImagePath)); 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(); return Convert.ToBase64String(arr); } catch (Exception ex) { return null; } }