[英]File Compression Type changes when converting image to base64
我有一个压缩类型为 CCITT T.6 的图像,它被转换为 base64 并发送到后端 API,其中 base64 字符串将被转换回原始图像并验证文件详细信息。 我的问题是当我将 base64 字符串转换回其原始图像时,图像的压缩类型现在已更改为 LZW。 将图像转换为 base64 字符串会改变其压缩类型吗? 如果是这样,我怎样才能保持文件的原始压缩类型。
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);
}
摆脱内存流并使用 Steeeve 建议的Convert.ToBase64String(File.ReadAllBytes(filepath))
似乎已经解决了这个问题。 从 base64 字符串重新生成图像后,图像压缩类型现在是一致的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.