Search code examples
powershellpng

Get the detail informations from a .png file in PowerShell


How can I get the detail informations from a specific .png file in PowerShell?
Like dimensions, bit depth and size.


Solution

  • You can get most of this information from the files extended properties like this:

    $path = 'D:\image.png'
    $shell = New-Object -COMObject Shell.Application
    $folder = Split-Path $path
    $file = Split-Path $path -Leaf
    $shellfolder = $shell.Namespace($folder)
    $shellfile = $shellfolder.ParseName($file)
    
    $width = 27
    $height = 28
    $Dimensions = 26
    $size = 1
    
    $shellfolder.GetDetailsOf($shellfile, $width)
    $shellfolder.GetDetailsOf($shellfile, $height)
    $shellfolder.GetDetailsOf($shellfile, $Dimensions)
    $shellfolder.GetDetailsOf($shellfile, $size)
    

    You can also get the size in other ways such as (Get-Item D:\image.png).Length / 1KB.

    The bit depth property doesn't seem to be listed in the extended properties though even though its available when you right click the file.

    Update Another option is to use .NET proper to avoid using COM:

    add-type -AssemblyName System.Drawing
    $png = New-Object System.Drawing.Bitmap 'D:\image.png'
    $png.Height
    $png.Width
    $png.PhysicalDimension
    $png.HorizontalResolution
    $png.VerticalResolution
    

    Update 2 The PixelFormat property gives you the bit depth.

    $png.PixelFormat
    

    The property is an enumeration of possible formats. You can view the complete list here.

    For example, Format32bppArgb is defined as

    Specifies that the format is 32 bits per pixel; 8 bits each are used for the alpha, red, green, and blue components.