zoukankan      html  css  js  c++  java
  • Image与Base64String的互转换

         public Image Base64ToImage(string base64String)
            {
                // Convert Base64 String to byte[]
                byte[] imageBytes = Convert.FromBase64String(base64String);
                MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
    
                // Convert byte[] to Image
                ms.Write(imageBytes, 0, imageBytes.Length);
                Image image = Image.FromStream(ms, true);
                return image;
            }
    
    
            public string CreateImgToBase64(string imagePath)
            {
                Bitmap bmp = (Bitmap)Image.FromFile(imagePath);
                MemoryStream stream = new MemoryStream();
                bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                stream.Position = 0;
                byte[] data = new byte[stream.Length];
                stream.Read(data, 0, (int)stream.Length);
                stream.Close();
    
                string base64String = string.Empty;
                try
                {
                    base64String = System.Convert.ToBase64String(data, 0, data.Length);
                }
                catch
                {
                    throw;
                }
    
                StreamWriter outFile;
    
                try
                {
                    outFile = new StreamWriter(string.Concat(imagePath, ".txt"), false, Encoding.ASCII);
                    outFile.Write(base64String);
                    outFile.Close();
    
                    return base64String;
                }
                catch
                {
                    throw;
                }
    
                bmp.Dispose();
            }
    
            public void CreateBase64StrToImage(string filePath)
            {
                StreamReader reader = new StreamReader(filePath);
                string str = reader.ReadToEnd();
    
                byte[] bitmapData = new byte[str.Length];
                bitmapData = Convert.FromBase64String(FixBase64ForImage(str));
                MemoryStream streamBitmap = new MemoryStream(bitmapData);
    
                Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));
                bitImage.Save(filePath.Substring(0, filePath.Length - 4), System.Drawing.Imaging.ImageFormat.Jpeg);
                bitImage.Dispose();
            }
    
            static string FixBase64ForImage(string image)
            {
                StringBuilder sbText =
                    new StringBuilder(image, image.Length);
    
                sbText.Replace("
    ", string.Empty);
                sbText.Replace(" ", string.Empty);
    
                return sbText.ToString();
            }
  • 相关阅读:
    Navigator对象
    Location对象
    History 对象
    计时器用法
    window对象
    某班的成绩出来了,现在老师要把班级的成绩打印出来,和 显示当前时间
    牛客练习赛14A(唯一分解定理)
    贪心法(举例子强行凑规律)
    线性筛(欧拉筛法)
    欧拉函数
  • 原文地址:https://www.cnblogs.com/ShaYeBlog/p/4199847.html
Copyright © 2011-2022 走看看