Mert
Mert

Reputation: 6582

bitmap file size vs byte[] size

bmp.ToByteArray(ImageFormat.Bmp).Length 3145782 int but file system shows as 2,25 MB (2.359.350 bytes) and Size on disk 2,25 MB (2.363.392 bytes)

Why there is difference and how can I determine correct size of bitmap in byte[] form?

    string appPath = Application.StartupPath;

    var bmp = new Bitmap(Image.FromFile(appPath + "\\Images\\Penguins.bmp"));

    public static byte[] ToByteArray(this Image image, ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, format);
            return ms.ToArray();
        }
    }

Windows 7 / NTFS

Upvotes: 4

Views: 3506

Answers (1)

Andrew Wilkinson
Andrew Wilkinson

Reputation: 10846

I suspect that's because the file on disk doesn't contain an alpha channel, but in memory it does. On disk it's 3 bytes per pixel, but in memory it uses 4.

2359350*4/3 is 3145800 which is only slightly more than the value you see. I expect the slight difference is because on disk there is a header, but that's not actually part of the image.

Upvotes: 2

Related Questions