C# base64 和图片互转

C# imgage图片转base64字符/base64字符串转图片另存成

//图片转为base64编码的字符串
 
protected string ImgToBase64String(string Imagefilename) 
{
try 
{ 
Bitmap bmp = new Bitmap(Imagefilename); 
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;
} 
}  
//base64编码的字符串转为图片 
protected Bitmap Base64StringToImage(string strbase64)
{ 
try
{ 
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr); 
Bitmap bmp = new Bitmap(ms);
bmp.Save(@"d:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(@"d:\"test.bmp", ImageFormat.Bmp);
 
//bmp.Save(@"d:\"test.gif", ImageFormat.Gif);
 
//bmp.Save(@"d:\"test.png", ImageFormat.Png);
ms.Close();
return bmp;
} 
catch (Exception ex)
{
return null; 
} 
}
2: 图片直接显示
string Base64String = "";//太长就不贴出来了
byte[] bytes = Convert.FromBase64String(Base64String); 
ImageTagId.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(bytes);//
3:转换成图片另存为然后图片显示出来
public System.Drawing.Image Base64ToImage(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String); 
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length); 
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
return image;
}
使用方法: 
Base64ToImage(dd).Save(Server.MapPath("Hello.jpg")); 
Image1.ImageUrl = "Hello.jpg";

4:直接转换存为路径下图片

string dd = "";//base64字符串
var bytes = Convert.FromBase64String(dd);
using (var imageFile = new FileStream(@"d:\Hello1.jpg", FileMode.Create)) 
{ 
imageFile.Write(bytes, 0, bytes.Length);
imageFile.Flush(); 
}

5://直接转换存为路径下图片

string base64Str=""/
byte[] bytes = System.Convert.FromBase64String(dd);
File.WriteAllBytes(@"d:\newfile.jpg", bytes); 
string filePath = @"d:\MyImage.jpg"; 
File.WriteAllBytes(filePath, Convert.FromBase64String(base64Str));

6:多张base64图片转换为图片另存为

public string Base64ToImage(string upimgPath, string base64String)
    {
      string goodspath = Server.MapPath(upimgPath);  //用来生成文件夹
      if (!Directory.Exists(goodspath)) 
      {
        Directory.CreateDirectory(goodspath);
      } 
      var imgPath = string.Empty;
      if (!string.IsNullOrEmpty(base64String)) 
      {
        var splitBase = base64String.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var item in splitBase)
        {
          var path = upimgPath + Guid.NewGuid() + ".jpg";
          string filePath = Server.MapPath(path);// Server.MapPath(upimgPath + Guid.NewGuid() + ".jpg");
          File.WriteAllBytes(filePath, Convert.FromBase64String(item));
          imgPath += path + ";";
        }
      } 
      else { imgPath = ";"; } 
      return imgPath.TrimEnd(';');
    }

使用方法:

va headimg="";

 Base64ToImage("UpLoadImg/HeadImage/", headimg)

 

posted @ 2018-11-20 08:51  yaphetsfang  阅读(2908)  评论(0编辑  收藏  举报