2006-03-01

GETTING IMAGE PROPERTIES WITH GDI+




LOCAL lcSource, lcInfo, lnWidth, lnHeight, lnHorRes, lnVerRes, lnPixForm
LOCAL lcpixform
m.lcSource = GETPICT()

LOCAL loImage AS gpImage OF FFC / _GdiPlus.vcx
m.loImage = NEWOBJECT("gpimage", HOME() + "ffc/_gdiplus.vcx")
m.loImage.CreateFromFile(m.lcSource)

m.lnWidth = m.loImage.ImageWidth
m.lnHeight = m.loImage.ImageHeight
m.lnHorRes = m.loImage.HorizontalResolution
m.lnVerRes = m.loImage.VerticalResolution
m.lnPixForm = m.loImage.PixelFormat
m.lcpixform = GetPixFormatName(m.lnPixForm)

m.lcInfo = "image : " + m.lcSource + CHR(13) + CHR(13) + ;
    "width : " + TRANSFORM(m.lnWidth) + " pixels" + CHR(13) + ;
    "height : " + TRANSFORM(m.lnHeight) + " pixels" + CHR(13) + ;
    "pixel format : " + m.lcpixform + CHR(13) + ;
    "hor. resol : " + TRANSFORM(m.lnHorRes) + " pixels/inch" + CHR(13) + ;
    "ver. resol : " + TRANSFORM(m.lnVerRes) + " pixels/inch" + CHR(13)

MESSAGEBOX(m.lcInfo, 64, "image information")
RETURN

PROCEDURE GetPixFormatName(npix)
    DO CASE
        CASE m.npix = 0x00030101
            RETURN "1bppindexed"
        CASE m.npix = 0x00030402
            RETURN "4bppindexed"
        CASE m.npix = 0x00030803
            RETURN "8bppindexed"
        CASE m.npix = 0x00101004
            RETURN "16bppgrayscale"
        CASE m.npix = 0x00021005
            RETURN "16bpprgb555"
        CASE m.npix = 0x00021006
            RETURN "16bpprgb565"
        CASE m.npix = 0x00061007
            RETURN "16bppargb1555"
        CASE m.npix = 0x00021808
            RETURN "24bpprgb"
        CASE m.npix = 0x00022009
            RETURN "32bpprgb"
        CASE m.npix = 0x0026200a
            RETURN "32bppargb"
        CASE m.npix = 0x000e200b
            RETURN "32bpppargb"
        CASE m.npix = 0x0010300c
            RETURN "48bpprgb"
        CASE m.npix = 0x001c400e
            RETURN "64bpppargb"
        OTHERWISE
            RETURN "unidentified"
    ENDCASE
ENDPROC


2 comments:

  1. Cesar

    Excellent as ever - small typo in code

    "Height : " + TRANSFORM(lnWidth)

    should be

    "Height : " + TRANSFORM(lnHeight)
    Hi Chris,
    Thanks for reporting ! I've already updated the text.
    Regards,
    Cesar

    ReplyDelete
  2. Thanks for this code. It is very usefull. Ca you get more information from an image file this way (camera to take the picture, lens aperture, etc.)?

    Thank you!
    Of course it is possible !
    We have a great sample in the GdiPlusX samples. Please download the samples project from GdiPlusX, and run the sample "ImageProps.Scx", or directly from the samples menu select "Image Properties". It uses the function "GetPropertyItem" to retrieve the metadata embedded in the picture file. Try the sample, selecting some digital pictures that you took with your camera, and you'll find some really cool info.
    Hope this helps !

    ReplyDelete