我正在使用 BitMiracle 的 LibTiff.Net 读取位图图像并返回作为 Base64String 嵌入文件中的 TIFF 字节 []。我注意到 Base64 字符串最终比我预期的要长很多,它的尾部是大量的“A”字符。在调试时,我看到 LibTiff 返回给我的 byte[] 末尾有几千个 0 值,这似乎不是图像本身的必要部分(据我所知)。
我在这里使用 BitMiracle 的示例代码进行转换: https ://bitmiracle.github.io/libtiff.net/html/075f57db-d779-48f7-9fd7-4ca075a01599.htm
不过,我不太明白在字节 [] 末尾会导致“垃圾”的原因。有什么想法吗?
编辑以添加代码 - GetTiffImageBytes() 在上面的链接中:
public void GenImage()
using (System.Drawing.Image frontImage = System.Drawing.Image.FromStream(file))//;
{
file.Close();
//Draw something
b = new Bitmap(frontImage);
Graphics graphics = Graphics.FromImage(b);
graphics.DrawString(data1, (Font)GlobalDict.FontDict["font1"], Brushes.Black, 200, 490);
graphics.DrawString(data2, (Font)GlobalDict.FontDict["font2"], Brushes.Black, 680, 400);
}
//Convert to TIF - requires BitMiracle.LibTiff.Classic
byte[] tiffBytes = GetTiffImageBytes(b, false);
return tiffBytes;
}
上面的调用是:
byte[] aFrontImage = MiscTools.GenImage(somestuff);
fileXML.WriteLine(" <FrontImage>" + System.Convert.ToBase64String(aFrontImage, 0, aFrontImage.Length) + "</FrontImage>");
所有的事情都说和做了,它运行良好,我们的应用程序可以读取生成的图像。我只是想缩小大小,因为其中一些文件可能有数万张图像。我有一些较旧的示例文件是通过另一种方法用一些 Base64 字符串手动创建的,这些字符串大小差不多,保存了我认为是垃圾的所有尾随字节。
正如有人评论的那样,一种选择可能是在转换之前读取 byte[] 并从末尾删除所有 0 值,但我试图弄清楚为什么会发生这种情况。
谢谢!