代码之家  ›  专栏  ›  技术社区  ›  donprecious iyeritufu

上载预览图像时获取“base-64字符数组或字符串的长度无效”

  •  0
  • donprecious iyeritufu  · 技术社区  · 6 年前

    这个 Base64String 使用的参数开始于:

    “数据:image/jpeg;base64,/9j/4aaqskzjrgabaqaaqabad/2wbdaaebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqebaqeb

    public bool UploadAvatar(string UserID, string Base64String)
    {
        if (string.IsNullOrEmpty(Base64String))
        {
            ReturnMessage = "Profile picture upload failed. No data from photo!";
            return false;
        }
    
        if (Base64ImageToFile(Base64String, filename, System.Drawing.Imaging.ImageFormat.Jpeg))
        {
            ReturnMessage = "Profile picture uploaded successfully!";
            return true;
        }
    
        ReturnMessage = "Failure uploading your profile picture!";
        return false;
    }
    
    public bool Base64ImageToFile(string Base64String, string SaveAs, System.Drawing.Imaging.ImageFormat Format)
    {
        try
        {
            //Remove this part "data:image/jpeg;base64,"
            Base64String = Base64String.Split(',')[1];
           //Base64String = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ......"
    
            //This is where the error occurs
    
            byte[] bytes = Convert.FromBase64String(Base64String);
    
            Image image;
            using (var ms = new MemoryStream(bytes))
            {
                image = Image.FromStream(ms);
            }
            image.Save(SaveAs, Format);
            return true;
        }
        catch (Exception ex)
        {
            ReturnMessage = ex.Message;
            return false;
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Prashant Pimpale Dila Gurung    6 年前

        public void GetImage(string imageString)
        {
            string imageStringToConvertIntoImage = "";
    
            // Check the Base64 string before remove the starting extension like: `data:image/jpeg;base64,`
            if (imageString.Contains(@"data:image/jpeg;base64,"))
            {
                imageStringToConvertIntoImage = imageString.Split(',')[1];
            }
            else
            {
                imageStringToConvertIntoImage = imageString;
            }
    
            // Image path where do you want to store images after convert
            string path = @"E:\\Temp\";
    
            // Check if path is exists or not, If not then create
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
    
            if (!string.IsNullOrEmpty(imageStringToConvertIntoImage))
            {
              // Replace - with +
                string converted = imageStringToConvertIntoImage.Replace('-', '+');
              // From above modified string replace _ with /
                converted = converted.Replace('_', '/');
    
                using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(imageStringToConvertIntoImage)))
                {
                    var pageFilePath = Path.Combine(path, string.Format(DateTime.Now.Ticks.ToString() + "-Image.jpg"));
                    // Draw image from memory stream and use Save method to save image file with specified format
                    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                    image.Save(pageFilePath, ImageFormat.Jpeg);
                }
            }
        }