开发者

determine jpeg filesize without actually saving the jpeg

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 fo开发者_如何学运维llowing 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;


Try loading it into memory without saving to disk. Look up MemoryStream.

Also, this answer might help: How to get the file size of a "System.Drawing.Image"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜