I have an image in PNG or BMP format. I would like to find out the filesize of that image after compressing it to a jpeg without saving the jpeg.
Up to now I did it the following way:
frameNormal.Save("temp.jpg", ImageFormat.Jpeg);
tempFile = new FileInfo("temp.jpg");
filesizeJPG = tempFile.Length;
But because of the slow disk access, the program takes too long. Is there a way to calculate the filesize of the newly created jpeg? Like converting the PNG in memory and read the size...
Any help is very much appreciated :-)
You can write the image data to a Stream
instead of supplying a filename:
using (MemoryStream mem = new MemoryStream())
{
frameNormal.Save(mem, ImageFormat.Jpeg);
filesizeJPG = mem.Length;
}
You can to something like this:
MemoryStream tmpMs = new MemoryStream();
frameNormal.Save(tmpMs, ImageFormat.Jpeg);
long fileSize = tmpMs.Length;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With