简体   繁体   中英

File Compression Type changes when converting image to base64

I have and image with a compression type of CCITT T.6 which is converted to base64 and sent to a backend API, where base64 string will be converted back to the original image and validate the file details. My problem is whenI convert the base64 string back to its original image, the compression type for the image has now changed to LZW. Does converting an image to a base64 string change its compression type? If so how can I keep the files original compression type.

string img = "";       
using (Image image = Image.FromFile(filepath))
{
    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        byte[] imageBytes = m.ToArray();

        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);
        img = base64String;
    }
}

LoadImage(img);

 public void LoadImage(string base64image)
 {
     byte[] bytes = Convert.FromBase64String(base64image);
     Image image;
     using (MemoryStream ms = new MemoryStream(bytes))
     {
         image = Image.FromStream(ms);
     }
     File.WriteAllBytes(filepath,bytes);
 }

Getting rid of the memory stream and using Convert.ToBase64String(File.ReadAllBytes(filepath)) suggested by Steeeve seems to have solved the problem. Image Compression types are now consistent after regenerating the image from a base64 string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM