简体   繁体   English

将图像转换为 base64 时文件压缩类型发生变化

[英]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.我有一个压缩类型为 CCITT T.6 的图像,它被转换为 base64 并发送到后端 API,其中 base64 字符串将被转换回原始图像并验证文件详细信息。 My problem is whenI convert the base64 string back to its original image, the compression type for the image has now changed to LZW.我的问题是当我将 base64 字符串转换回其原始图像时,图像的压缩类型现在已更改为 LZW。 Does converting an image to a base64 string change its compression type?将图像转换为 base64 字符串会改变其压缩类型吗? 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.摆脱内存流并使用 Steeeve 建议的Convert.ToBase64String(File.ReadAllBytes(filepath))似乎已经解决了这个问题。 Image Compression types are now consistent after regenerating the image from a base64 string.从 base64 字符串重新生成图像后,图像压缩类型现在是一致的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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