How do I get the current memory size of an image?

M

moss

Hi people,
my question is this: How can I get the byte size of an in-memory Bitmap?
For example, I want to know that a 100 KB JPEG takes a 2.25 MB of memory
space when uncompressed... I tried saving an image to a memory stream and
then converting the stream to a byte array and getting the length of the
array, but that doesn't produce the correct results... Any ideas?
 
A

Arne Vajhøj

my question is this: How can I get the byte size of an in-memory Bitmap?
For example, I want to know that a 100 KB JPEG takes a 2.25 MB of memory
space when uncompressed... I tried saving an image to a memory stream and
then converting the stream to a byte array and getting the length of the
array, but that doesn't produce the correct results... Any ideas?

You can only do that calculation for image format that does not use
compression.

JPEG uses compression. That means that a X KB JPEG will expand
to different sizes of bitmaps depending on the content.

Arne
 
F

Family Tree Mike

Hi people,
my question is this: How can I get the byte size of an in-memory Bitmap?
For example, I want to know that a 100 KB JPEG takes a 2.25 MB of memory
space when uncompressed... I tried saving an image to a memory stream and
then converting the stream to a byte array and getting the length of the
array, but that doesn't produce the correct results... Any ideas?

Maybe I'm naive, but could you not just get the size of the image
(image.Size.Width, image.Size.Height), and multiply by the bytes per
pixel from the image.PixelFormat property?
 
P

Peter Duniho

Family said:
Maybe I'm naive, but could you not just get the size of the image
(image.Size.Width, image.Size.Height), and multiply by the bytes per
pixel from the image.PixelFormat property?

Not naïve at all IMHO. Some mapping does need to be done from the pixel
format to the actual bytes-per-pixel, and of course there are additional
bytes of overhead beyond the raw image data (stride, meta-data, a
palette for some images, etc.) but for most scenarios the basic
calculation should be sufficient.

Pete
 
F

Family Tree Mike

Not naïve at all IMHO. Some mapping does need to be done from the pixel
format to the actual bytes-per-pixel, and of course there are additional
bytes of overhead beyond the raw image data (stride, meta-data, a
palette for some images, etc.) but for most scenarios the basic
calculation should be sufficient.

Pete

Thanks Pete. I thought I was missing something here.
 
M

moss

Maybe I'm naive, but could you not just get the size of the image
(image.Size.Width, image.Size.Height), and multiply by the bytes per
pixel from the image.PixelFormat property?

Thanks man, it's exactly what I need.
 
G

Göran Andersson

Family said:
Maybe I'm naive, but could you not just get the size of the image
(image.Size.Width, image.Size.Height), and multiply by the bytes per
pixel from the image.PixelFormat property?

That's exactly what you have to do if you want something that is close
to accurate.

A JPEG is generally loaded as a 32bpp image, so the size of the image
data is at least:

width * height * 4

Then there may be padding between each scan line to align them on an
even address, but that is likely to be zero with a 32bpp image.

There is of course also some more data in the Image object to keep track
of the image, and there might also be EXIF data from the JPEG file, but
it's of course the image data itself that takes up most of the space.

-

If you know the quality setting used when the image is saved, you could
make a rough estimation from the JPEG file size. however, unless the
image content is very uniform, the variation is pretty big from one
image to another. A varation of +-50% is not uncommon.
 
Joined
Jan 6, 2014
Messages
1
Reaction score
0
I have a question about "JPG current memory size" vs "JPG disk size". This has haunted me years and now I decided to find out at least some sort of information about the difference.

Basically I am confused when I open a flatten JPG file with IrfanView for example, and it gives me 2 sizes:
Current memory size: 1.54Mb (for example)
Disk size: 253Kb (for example)
Original + current size: 672 x 800 px


What exactly IS "Current memory size"? Is it some hidden format? Or is the size of a file which can be still edited... somehow, even when flatten in Photoshop?? Because usually PSD files are the same size. How can this almost 8x difference exist between those 2 variants?


Any knowledge?
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top