[英]convert image to base64 and check size
I am using the following c# code to convert an image file to a base64 string 我使用以下c#代码将图像文件转换为base64字符串
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
var base64 = Convert.ToBase64String(buffer);
}
How can I test the before and after size? 如何测试前后尺寸? ie. 即。 the image file size and the size of the base 64 string. 图像文件大小和基本64字符串的大小。 I want to check if I am winning or losing by using converting it. 我想通过转换它来检查我是赢还是输。
You can calculate it by using simple math. 您可以使用简单的数学计算它。 One character of base64 represents 6 bits, and therefore four characters represent three bytes. base64的一个字符代表6位,因此四个字符代表三个字节。 So you get 3/4 bytes per character. 所以你得到每个字符3/4字节。 Which gives: 这使:
int base64EncodedSize = 4 * originalSizeInBytes / 3;
Depending on how the data is padded it might be off by a character or two but that shouldn't make a difference. 根据数据的填充方式,它可能会被一两个字符关闭,但这不会产生影响。
Also, if you suspect base64 might be more efficient, what on earth are you comparing it with? 另外,如果您怀疑base64可能更有效率,那么您与它进行比较的是什么? Compared to raw binary, it always causes a 33% increase in size. 与原始二进制文件相比,它总是导致大小增加33%。
You're losing - the base64 string will use more bytes to store than would your original image. 你输了 - base64字符串将使用比原始图像更多的字节来存储。 Possibly a lot more, if this is all in-memory: strings in .Net are unicode, so they use twice as many bytes as an ASCII-encoded string would. 也许更多的事情 ,如果这是所有内存:在.net中的字符串是unicode的,所以他们用两倍的字节将一个ASCII编码的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.