• 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;
    			}
    		}
    
    		//threeebase64编码的字符串转为图片
    		protected Bitmap Base64StringToImage(string strbase64)
    		{
    			try
    			{
    				byte[] arr = Convert.FromBase64String(strbase64);
    				MemoryStream ms = new MemoryStream(arr);
    				Bitmap bmp = new Bitmap(ms);
    
    				 bmp.Save(@"d:	est.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;
    			}
    		}
    
    //图片转为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;
    			}
    		}
    
    		//threeebase64编码的字符串转为图片
    		protected Bitmap Base64StringToImage(string strbase64)
    		{
    			try
    			{
    				byte[] arr = Convert.FromBase64String(strbase64);
    				MemoryStream ms = new MemoryStream(arr);
    				Bitmap bmp = new Bitmap(ms);
    
    				 bmp.Save(@"d:	est.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);//
    
    //    
    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 = "";//太长就不贴出来了
    
                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=""/  
      
    e[] bytes = System.Convert.FromBase64String(dd);  
      
           File.WriteAllBytes(@"d:
    ewfile.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)
    
    
  • 相关阅读:
    「golang」go-micro指定consul地址
    「postgres」无数据则插入,有数据不做插入
    「postgres」导出数据以及分割数据
    「postgre」调整表结构
    「postgre」查看DB的物理占用空间
    「postgre」INT最大值
    「postgre」服务重启
    「golang」关于TIME_WAIT优化
    深入理解Spring Boot属性配置文件
    springboot 中的commandLineRunners接口
  • 原文地址:https://www.cnblogs.com/chenmfly/p/7380552.html
Copyright © 2020-2023  润新知