[英]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.