获取图像的文件大小

dav*_*eah 3 c# image

我搜索以获取C#中图像的文件大小(以字节为单位):

我在base64字符串中获取图像,但在这种格式中,我觉得它无法获得文件大小.

所以我在像这样的图像对象中进行转换:

// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(imageSrc);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image imageObj = Image.FromStream(ms, true);
Run Code Online (Sandbox Code Playgroud)

但我无法获得文件大小.我可以有尺寸或类型,但不能有文件大小.

你对此有所了解吗?

ppe*_*rov 8

您已经拥有图像的大小(以字节为单位),您可以这样得到它:

imageBytes.Length

据我所知,您不希望将其转换为其他格式,只需保存即可,因此您可以将字节直接保存到文件中,这是最简单的解决方案:

File.WriteAllBytes(string yourpath, byte[] yourbytes)
Run Code Online (Sandbox Code Playgroud)

  • +1.请注意,它是原始文件的大小.如果您将字节加载到Image中而不是保存它,则由于文件格式/压缩不同,大小可能会有所不同. (2认同)