Tipx
Tipx

Reputation: 7505

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.

Upvotes: 3

Views: 5309

Answers (3)

Paul Sasik
Paul Sasik

Reputation: 81459

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.

Upvotes: 5

Alexander Boychenko
Alexander Boychenko

Reputation: 276

var stream = new MemoryStream()
Bitmap.Save(Stream, ImageFormat)

Does it what you need?

Upvotes: 1

usr
usr

Reputation: 171178

Bitmap does not support encoded in-memory storage. It is always unencoded (see the PixelFormat enum). Problably you need to write your own wrapper class/abstraction, or give up on that idea.

Upvotes: 3

Related Questions