Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the file size of an Image

Tags:

c#

image

I search to get the file size (in bytes) of an image in 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.

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?

like image 569
david yeah Avatar asked Jan 14 '23 20:01

david yeah


1 Answers

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)
like image 156
ppetrov Avatar answered Jan 21 '23 21:01

ppetrov