简体   繁体   English

获取图像的文件大小

[英]Get the file size of an Image

I search to get the file size (in bytes) of an image in C# : 我搜索以获取C#中图像的文件大小(以字节为单位):

I get the image in a base64 string, but in this format I have the impression it's no possible to get the file size. 我在base64字符串中获取图像,但在这种格式中,我觉得它无法获得文件大小。

So I transforme it in an image object like this : 所以我在这样的图像对象中进行转换:

// 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);

But i'm not able to get the file size. 但我无法获得文件大小。 I can have the dimensions or the type but not the file size. 我可以有尺寸或类型,但不能有文件大小。

Have you got an idea on that please? 你对此有所了解吗?

You already have the size of your image in bytes, you can get it like this: 您已经拥有图像的大小(以字节为单位),您可以这样得到它:

imageBytes.Length

As I understood, you don't want to convert it to another format, just save it, so you can save your bytes directly to your file, the simplest solution: 据我所知,您不希望将其转换为其他格式,只需保存即可,因此您可以将字节直接保存到文件中,这是最简单的解决方案:

File.WriteAllBytes(string yourpath, byte[] yourbytes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM