Detect jpeg files without actually saving jpeg

I have a PNG or BMP image. I would like to know the file size of this image after compressing it in jpeg without saving jpeg .

So far I have done it as follows:

frameNormal.Save("temp.jpg", ImageFormat.Jpeg); tempFile = new FileInfo("temp.jpg"); filesizeJPG = tempFile.Length; 

But due to the slow access to the disk, the program takes too much time. Is there a way to calculate the file size of a newly created jpeg? Like converting PNG to memory and reading size ...

Any help is much appreciated :-)

+4
source share
3 answers

You can write image data to Stream instead of giving the file name:

 using (MemoryStream mem = new MemoryStream()) { frameNormal.Save(mem, ImageFormat.Jpeg); filesizeJPG = mem.Length; } 
+7
source

You can do something like this:

 MemoryStream tmpMs = new MemoryStream(); frameNormal.Save(tmpMs, ImageFormat.Jpeg); long fileSize = tmpMs.Length; 
+3
source

Try loading it into memory without saving to disk. Check out MemoryStream .

Additionally, this answer may help: How to get the file size of "System.Drawing.Image"

+2
source

Source: https://habr.com/ru/post/1347719/


All Articles