facebooktwittermenuarrow-up

GemBox Support Forum

Get image disk size from pdf

Hello,

I used the tutorialcode for getting images from a pdf:

using GemBox.Pdf;
using GemBox.Pdf.Content;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("ExportImages.pdf"))
            // Iterate through PDF pages and through each page's content elements.
            foreach (var page in document.Pages)
                foreach (var contentElement in page.Content.Elements.All())
                    if (contentElement.ElementType == PdfContentElementType.Image)
                    {
                        // Export an image content element to selected image format.
                        var imageContent = (PdfImageContent)contentElement;
                        imageContent.Save("Export Images.jpeg");
                        return;
                    }
    }
}

Now I want to get the size of the image in the pdf, so of the imageContent variable.
Does the PdfImageContent have like a size or space usage variable, or do I need it with something like this ```
System.Runtime.InteropServices.Marshal.SizeOf(x)


Greetings brian

Hi,

You can get the size of both uncompressed and compressed image using the following code:

var image = imageContent.Image;

int uncompressedImageSizeInBytes = 0;
if (image.ColorSpace != null && (image.BitsPerComponent != null || image.ImageMask))
    uncompressedImageSizeInBytes = (image.Width * image.Height * (image.BitsPerComponent ?? 1) * image.ColorSpace.ColorantCount) / 8;

int compressedImageSizeInBytes = image.GetDictionary().Stream.Length;

Note that these sizes mostly won’t be equal to the size of the file to which the image was extracted because PdfImageContent might specify additional transformations (like rotation, scaling, etc.) and because image data stored inside the PDF file is usually compressed differently than in .PNG or .JPEG files.

Regards,
Stipo

1 Like

Thank you very much for the quick reply :slight_smile:

Is the output size in bytes?

Hi,

Yes, sizes from the provided code snippet are in bytes (also emphasized in variable names).

Regards,
Stipo

1 Like