K. Torrance
K. Torrance

Reputation: 129

C# Compressed Size of a Bitmap without savig it

I have a simple C# script that generates a Bitmap, saves it to the hard drive (PNG), checks the file Size and based on the size of the file it will move or delete the image.

Bitmap bmp = new Bitmap(1024, 768);
bmp.save(@"C:\User\...\image.png");

FileInfo fi = new FileInfo (@"C:\User\...\image.png");

if (fi.length > 50000){
//do Something
}

Is there any other way to get the compressed (RLE) size of the Bitmap without storing it locally?

Upvotes: 2

Views: 87

Answers (1)

Nasreddine
Nasreddine

Reputation: 37858

You can save the image to a MemoryStream instead:

Bitmap bmp = new Bitmap(1024, 768);
using (var ms = new MemoryStream())
{
    bmp.Save(ms, ImageFormat.Png);
    if (ms.Length > 50000){
        //do Something
    }
}

Upvotes: 5

Related Questions