Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the file size of a "System.Drawing.Image"

I am currently writing a system that stores meta data for around 140,000 ish images stored within a legacy image library that are being moved to cloud storage. I am using the following to get the jpg data...

System.Drawing.Image image = System.Drawing.Image.FromFile("filePath"); 

Im quite new to image manipulation but this is fine for getting simple values like width, height, aspect ratio etc but what I cannot work out is how to retrieve the physical file size of the jpg expressed in bytes. Any help would be much appreciated.

Thanks

Final solution including an MD5 hash of the image for later comparison

System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);  if (image != null) {   int width = image.Width;   int height = image.Height;   decimal aspectRatio = width > height ? decimal.divide(width, height) : decimal.divide(height, width);     int fileSize = (int)new System.IO.FileInfo(filePath).Length;    using (System.IO.MemoryStream stream = new System.IO.MemoryStream(fileSize))   {     image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);     Byte[] imageBytes = stream.GetBuffer();     System.Security.Cryptography.MD5CryptoServiceProvider provider = new System.Security.Cryptography.MD5CryptoServiceProvider();     Byte[] hash = provider.ComputeHash(imageBytes);      System.Text.StringBuilder hashBuilder = new System.Text.StringBuilder();      for (int i = 0; i < hash.Length; i++)     {       hashBuilder.Append(hash[i].ToString("X2"));     }      string md5 = hashBuilder.ToString();   }    image.Dispose();  } 
like image 853
Nick Allen Avatar asked Oct 21 '08 10:10

Nick Allen


People also ask

How do you calculate the file size of an image?

File size can be expressed as the resolution (the image width multiplied by the image height) multiplied by the bit depth (the number of bits needed to store colours).

How do you determine a file size?

Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

How do you find the byte size of a picture?

1. Multiply the width and height of the image, in pixels, to get the total pixel count. 2. Multiply the total pixel count by 3 to get the image size in bytes.

How do I calculate bitmap file size?

If the pixel dimensions are given, multiply them by each other and the bit depth to determine the number of bits in an image file. For instance, if a 24-bit image is captured with a digital camera with pixel dimensions of 2,048 x 3,072, then the file size equals (2048 x 3072 x 24)/8, or 18,874,368 bytes.


1 Answers

If you get your image directly from file, you can use the following code to get size of original file in bytes.

 var fileLength = new FileInfo(filePath).Length;  

If you get your image from other source, like getting one bitmap and composing it with other image, like adding watermark you will have to calculate size in run-time. You can't just use original file size, because compressing may lead to different size of output data after modification. In this case, you can use MemoryStream to save image to:

long jpegByteSize; using (var ms = new MemoryStream(estimatedLength)) // estimatedLength can be original fileLength {     image.Save(ms, ImageFormat.Jpeg); // save image to stream in Jpeg format     jpegByteSize = ms.Length;  } 
like image 146
Ilya Ryzhenkov Avatar answered Sep 20 '22 17:09

Ilya Ryzhenkov