Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode a Bitmap as a Jpeg

First, this question is NOT about "how to save a Bitmap as a jpeg on your disk?"

I can't find (or think of) a way of converting a Bitmap with Jpeg compression, but keep it as a Bitmap object. MSDN clearly shows how to save a Bitmap as a JPEG, but what I'm looking for is how to apply the encoding/compression to the Bitmap object, so I can still pass it around, in my code, without referencing the file.

One of the reasons behind that would be a helper class that handles bitmap, but shouldn't be aware of the persistency method used.

like image 551
Tipx Avatar asked Jan 23 '26 20:01

Tipx


1 Answers

All images are bitmaps when loaded into program memory. Specific compressions are typically utilized when writing to disk, and decompressing when reading from disk.

If you're worried about the in-memory footprint of an image you could zip-compress the bytes and pass the byte array around internally. Zipping would be good for lossless compression of an image. Don't forget that many image compressions have different levels of losiness (sp?) In other words, the compression throws away data to store the image in the smallest number of bytes possible.

De/compression is also a performance tradeoff in that you're trading memory footprint for processing time. And in any case, unless you get really fancy, the image does need to be a bitmap if you need to manipulate it in any way.

Here is an answer for a somewhat similar question which you might find interesting.

like image 106
Paul Sasik Avatar answered Jan 26 '26 10:01

Paul Sasik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!