I have a struct with an array of images :
public struct ObjectImages
{
public System.Drawing.Image[] _images;
public ObjectImages(System.Drawing.Image[] images)
{
_images = images;
}
}
If I put two images of 10 kb in the object and then try to serialize, I found that my memory stream target have 160kb. I verified,each image have ~10-11kb.
ObjectImages o = new ObjectImages(x); // where x is an array of images from my webcam
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, o);
I found that really strange! Can you please tell me if you know where is the problem?
Well, when you see the image on your hard disk, it is compressed – for example as PNG or JPEG file. When you load the image in C#, it is decompressed, so the pixels can be rendered. So in reality, the image consists of more bytes than are stored on your hard disk.
The C# object contains the uncompressed data, so roughly (for RGBA images), the size in memory should be
(4 * width * height) + further dataneeded by .NET. The binary formatter does not save the image (as PNG or JPEG), it saves the object that represents the image.