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

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;
 }

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)