Sign in to follow this  
Followers 0
ChrisL

GDIPlus Setting Meta data

6 posts in this topic

I'm trying to set the meta data in an image file, I can't get it to work and wondered if someone might be able to point me in the right direction..

#include <GDIPlus.au3>

Global Const $PropertyTagImageTitle = 0x0320 
Global Const $PropertyTagEquipMake = 0x010f
Global Const $PropertyTagEquipModel = 0x0110
Global Const $tagPropertyItem = "long id; long length; int Type; ptr value"

Global Const $PropertyTagTypeByte = 1
Global Const $PropertyTagTypeASCII = 2
Global Const $PropertyTagTypeShort = 3
Global Const $PropertyTagTypeLong = 4
Global Const $PropertyTagTypeRational = 5
Global Const $PropertyTagTypeUndefined = 7
Global Const $PropertyTagTypeSLong = 9
Global Const $PropertyTagTypeSRational = 10

 _GDIPlus_Startup ()

 $hFile = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\TestImport.jpg")
 
_GDIPlus_SetMetaData($hFile,"My Camera Model",$PropertyTagEquipMake)

_GDIPlus_ImageSaveToFile($hFile,@desktopDir & "\New.jpg")

_GDIPlus_ImageDispose($hFile)

_GDIPlus_Shutdown()


Func _GDIPlus_SetMetaData($hHandle,$vStr,$iD)
    
    $Struct_Meta = DllstructCreate($tagPropertyItem)
    DllStructSetData($Struct_Meta,"ID",$iD)
    DllStructSetData($Struct_Meta,"Length",StringLen($vStr) +1)
    DllStructSetData($Struct_Meta,"Type",$PropertyTagTypeASCII)
    DllStructSetData($Struct_Meta,"Value",$vStr)
 
    $aResult = DllCall($ghGDIPDll, "int", "GdipSetPropertyItem", "hwnd", $hHandle, "long*", DllStructGetPtr($Struct_Meta))
    If @error Then 
        $Struct_Meta = 0
        Return SetError(@error, @extended, False)
    EndIf
    
    $Struct_Meta = 0
    Return SetError($aResult[0], 0, $aResult[0] = 0)
    
 EndFunc

http://msdn.microsoft.com/en-us/library/ms533832(VS.85).aspx

Share this post


Link to post
Share on other sites



Hi!

I was not able to get it to work (At least I don't think so) but I've fixed some errors:

#include <GDIPlus.au3>

Global Const $PropertyTagImageTitle = 0x0320
Global Const $PropertyTagEquipMake = 0x010f
Global Const $PropertyTagEquipModel = 0x0110
; Changed long to ulong and int to ushort
Global Const $tagPropertyItem = "ulong id; ulong length; ushort Type; ptr value"

Global Const $PropertyTagTypeByte = 1
Global Const $PropertyTagTypeASCII = 2
Global Const $PropertyTagTypeShort = 3
Global Const $PropertyTagTypeLong = 4
Global Const $PropertyTagTypeRational = 5
Global Const $PropertyTagTypeUndefined = 7
Global Const $PropertyTagTypeSLong = 9
Global Const $PropertyTagTypeSRational = 10

_GDIPlus_Startup ()

$hFile = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\300.jpg")

_GDIPlus_SetMetaData($hFile,"My Camera Model",$PropertyTagEquipMake)

_GDIPlus_ImageSaveToFile($hFile,@desktopDir & "\New.jpg")

_GDIPlus_ImageDispose($hFile)

_GDIPlus_Shutdown()


Func _GDIPlus_SetMetaData($hHandle,$vStr,$iD)
   ; Store strings in arrays
    $MemString=DllStructCreate("char[255];")
    DllStructSetData($MemString,1,$vStr)
    
    $Struct_Meta = DllstructCreate($tagPropertyItem)
    DllStructSetData($Struct_Meta,"ID",$iD)
    DllStructSetData($Struct_Meta,"Length",StringLen($vStr) +1)
    DllStructSetData($Struct_Meta,"Type",$PropertyTagTypeASCII)
    DllStructSetData($Struct_Meta,"Value",DllStructGetPtr($MemString))

    $aResult = DllCall($ghGDIPDll, "int", "GdipSetPropertyItem", "hwnd", $hHandle, "long*", DllStructGetPtr($Struct_Meta))
    If @error Then
;~       MsgBox(0,"","")
        $Struct_Meta = 0
        Return SetError(@error, @extended, False)
    EndIf
    
    $Struct_Meta = 0
    Return SetError($aResult[0], 0, $aResult[0] = 0)
    
EndFunc

What I changed was, long to ulong, most likely not the issue, but still it's good to do things properly. int Type to ushort Type, this was important int is 4 bytes, ushort is 2 (would make the GDI+ read to few bytes).

The most important thing is how you saved the string, you were trying to save the entire string as a ptr, in C strings are saved in arrays therefore you need to create a struct with a byte[] array in it and then store the pointer to that struct in the Value property.

Hope I cleared up some things :P


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Digging up an old post I started a few years ago, I had to revisit this setting of exif meta data and I got it working today, so here is how you do it...

#include <GDIPlus.au3>

Global Const $PropertyTagTypeByte = 1
Global Const $PropertyTagTypeASCII = 2
Global Const $PropertyTagTypeShort = 3
Global Const $PropertyTagTypeLong = 4
Global Const $PropertyTagTypeRational = 5
Global Const $PropertyTagTypeUndefined = 7
Global Const $PropertyTagTypeSLong = 9
Global Const $PropertyTagTypeSRational = 10

_GDIPlus_Startup ()

$hFile = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\Photo.jpg")

_GDIPlus_SetMetaData($hFile,"Make", "Any Old Camera")
_GDIPlus_SetMetaData($hFile,"Author", "Chris Lambert")
_GDIPlus_SetMetaData($hFile,"Software", "Chris's Application")

_GDIPlus_ImageSaveToFile($hFile,@desktopDir & "\New.jpg")

_GDIPlus_ImageDispose($hFile)

_GDIPlus_Shutdown()


Func _GDIPlus_SetMetaData($hHandle,$sTagName, $vStr)

    Local $tagPropertyItem = "ulong id; ulong length; ushort Type; ptr value"
    Local $Struct_String, $Struct_Meta, $aResult, $PropertyTagType

    Switch $sTagName

        Case "ImageWidth"
            $ID = 0x100
            $PropertyTagType = $PropertyTagTypeShort

        Case "ImageLength"
            $ID = 0x101
            $PropertyTagType = $PropertyTagTypeShort

        Case "BitsPerSample"
            $ID = 0x102
            $PropertyTagType = $PropertyTagTypeShort

        Case "Compression"
            $ID = 0x103
            $PropertyTagType = $PropertyTagTypeShort

        Case "PhotometricInterpretation"
            $ID = 0x106
            $PropertyTagType = $PropertyTagTypeShort

        Case "Orientation"
            $ID = 0x112
            $PropertyTagType = $PropertyTagTypeShort

        Case "SamplesPerPixel"
            $ID = 0x115
            $PropertyTagType = $PropertyTagTypeShort

        Case "PlanarConfiguration"
            $ID = 0x11C
            $PropertyTagType = $PropertyTagTypeShort

        Case "YCbCrSubSampling"
            $ID = 0x212
            $PropertyTagType = $PropertyTagTypeShort

        Case "YCbCrPositioning"
            $ID = 0x213
            $PropertyTagType = $PropertyTagTypeShort

        Case "XResolution"
            $ID = 0x11A
            $PropertyTagType = $PropertyTagTypeRational

        Case "YResolution"
            $ID = 0x11B
            $PropertyTagType = $PropertyTagTypeRational

        Case "ResolutionUnit"
            $ID = 0x296
            $PropertyTagType = $PropertyTagTypeShort

        Case "StripOffsets"
            $ID = 0x111
            $PropertyTagType = $PropertyTagTypeShort

        Case "RowsPerStrip"
            $ID = 0x116
            $PropertyTagType = $PropertyTagTypeShort

        Case "StripByteCounts"
            $ID = 0x117
            $PropertyTagType = $PropertyTagTypeShort

        Case "JPEGInterchangeFormat"
            $ID = 0x201
            $PropertyTagType = $PropertyTagTypeLong

        Case "JPEGInterchangeFormatLength"
            $ID = 0x202
            $PropertyTagType = $PropertyTagTypeLong

        Case "TransferFunction"
            $ID = 0x12D
            $PropertyTagType = $PropertyTagTypeShort

        Case "WhitePoint"
            $ID = 0x13E
            $PropertyTagType = $PropertyTagTypeRational

        Case "PrimaryChromaticities"
            $ID = 0x13F
            $PropertyTagType = $PropertyTagTypeRational

        Case "YCbCrCoefficients"
            $ID = 0x211
            $PropertyTagType = $PropertyTagTypeRational


        Case "ReferenceBlackWhite"
            $ID = 0x214
            $PropertyTagType = $PropertyTagTypeRational

        Case "DateTime"
            $ID = 0x132
            $PropertyTagType = $PropertyTagTypeASCII

        Case "ImageDescription"
            $ID = 0x10E
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Make"
            $ID = 0x10F
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Model"
            $ID = 0x110
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Software"
            $ID = 0x131
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Artist", "Author"
            $ID = 0x13B
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Copyright"
            $ID = 0x8298
            $PropertyTagType = $PropertyTagTypeASCII

        Case Else
            Return SetError(1, -1, False)
    EndSwitch

   ; Store string in array
    $Struct_String=DllStructCreate("char[" & StringLen($vStr) +1 & "];")
    DllStructSetData($Struct_String,1,$vStr)

    $Struct_Meta = DllstructCreate($tagPropertyItem)
    DllStructSetData($Struct_Meta,"ID",$ID)
    DllStructSetData($Struct_Meta,"Length",StringLen($vStr) +1)
    DllStructSetData($Struct_Meta,"Type",$PropertyTagType)
    DllStructSetData($Struct_Meta,"Value",DllStructGetPtr($Struct_String))

    $aResult = DllCall($ghGDIPDll, "int", "GdipSetPropertyItem", "hwnd", $hHandle, "ptr", DllStructGetPtr($Struct_Meta))
    If @error Then Return SetError(@error, @extended, False)

    Return SetError($aResult[0], 0, $aResult[0] = 0)

EndFunc

Share this post


Link to post
Share on other sites

Thanks! Exactly what i was looking for!

PS: just a small remak: it doesn't work with PNG files (only JPG)

Share this post


Link to post
Share on other sites

I'm trying to extend this function to work also with other meta data, using these parameters:

http://msdn.microsoft.com/en-us/library/ms534416(v=vs.85).aspx#_gdiplus_constant_propertytagexifdtorig

But some Exif are not modified. Do you know if there are some limits following this way?

This is the updated code I'm using:

Func _GDIPlus_SetMetaData($hHandle, $sTagName, $vStr)
    Local $tagPropertyItem = "ulong id; ulong length; ushort Type; ptr value"
    Local $Struct_String, $Struct_Meta, $aResult, $PropertyTagType
    Local Const $PropertyTagTypeByte = 1
    Local Const $PropertyTagTypeASCII = 2
    Local Const $PropertyTagTypeShort = 3
    Local Const $PropertyTagTypeLong = 4
    Local Const $PropertyTagTypeRational = 5
    Local Const $PropertyTagTypeUndefined = 7
    Local Const $PropertyTagTypeSLong = 9
    Local Const $PropertyTagTypeSRational = 10

    Switch $sTagName
        Case "ImageWidth"
            $ID = 0x100
            $PropertyTagType = $PropertyTagTypeShort

        Case "ImageLength"
            $ID = 0x101
            $PropertyTagType = $PropertyTagTypeShort

        Case "BitsPerSample"
            $ID = 0x102
            $PropertyTagType = $PropertyTagTypeShort

        Case "Compression"
            $ID = 0x103
            $PropertyTagType = $PropertyTagTypeShort

        Case "PhotometricInterpretation"
            $ID = 0x106
            $PropertyTagType = $PropertyTagTypeShort

        Case "Orientation"
            $ID = 0x112
            $PropertyTagType = $PropertyTagTypeShort

        Case "SamplesPerPixel"
            $ID = 0x115
            $PropertyTagType = $PropertyTagTypeShort

        Case "PlanarConfiguration"
            $ID = 0x11C
            $PropertyTagType = $PropertyTagTypeShort

        Case "YCbCrSubSampling"
            $ID = 0x212
            $PropertyTagType = $PropertyTagTypeShort

        Case "YCbCrPositioning"
            $ID = 0x213
            $PropertyTagType = $PropertyTagTypeShort

        Case "XResolution"
            $ID = 0x11A
            $PropertyTagType = $PropertyTagTypeRational

        Case "YResolution"
            $ID = 0x11B
            $PropertyTagType = $PropertyTagTypeRational

        Case "ResolutionUnit"
            $ID = 0x296
            $PropertyTagType = $PropertyTagTypeShort

        Case "StripOffsets"
            $ID = 0x111
            $PropertyTagType = $PropertyTagTypeShort

        Case "RowsPerStrip"
            $ID = 0x116
            $PropertyTagType = $PropertyTagTypeShort

        Case "StripByteCounts"
            $ID = 0x117
            $PropertyTagType = $PropertyTagTypeShort

        Case "JPEGInterchangeFormat"
            $ID = 0x201
            $PropertyTagType = $PropertyTagTypeLong

        Case "JPEGInterchangeFormatLength"
            $ID = 0x202
            $PropertyTagType = $PropertyTagTypeLong

        Case "TransferFunction"
            $ID = 0x12D
            $PropertyTagType = $PropertyTagTypeShort

        Case "WhitePoint"
            $ID = 0x13E
            $PropertyTagType = $PropertyTagTypeRational

        Case "PrimaryChromaticities"
            $ID = 0x13F
            $PropertyTagType = $PropertyTagTypeRational

        Case "YCbCrCoefficients"
            $ID = 0x211
            $PropertyTagType = $PropertyTagTypeRational

        Case "ReferenceBlackWhite"
            $ID = 0x214
            $PropertyTagType = $PropertyTagTypeRational

        Case "DateTime"
            $ID = 0x132
            $PropertyTagType = $PropertyTagTypeASCII

        Case "ImageDescription"
            $ID = 0x10E
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Make"
            $ID = 0x10F
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Model"
            $ID = 0x110
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Software"
            $ID = 0x131
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Artist", "Author"
            $ID = 0x13B
            $PropertyTagType = $PropertyTagTypeASCII

        Case "ImageTitle"
            $ID = 0x320
            $PropertyTagType = $PropertyTagTypeASCII

        Case "Copyright"
            $ID = 0x8298
            $PropertyTagType = $PropertyTagTypeASCII

        Case "ExposureTime"
            $ID = 0x829A
            $PropertyTagType = $PropertyTagTypeRational

        Case "FNumber"
            $ID = 0x829D
            $PropertyTagType = $PropertyTagTypeRational

        Case "ISO"
            $ID = 0x8827
            $PropertyTagType = $PropertyTagTypeShort

        Case "ExifVersion"
            $ID = 0x9000
            $PropertyTagType = $PropertyTagTypeUndefined

        Case "DateTimeOriginal"
            $ID = 0x9003
            $PropertyTagType = $PropertyTagTypeASCII

        Case "DateTimeDigitized"
            $ID = 0x9004
            $PropertyTagType = $PropertyTagTypeASCII

        Case "ExposureBiasValue"
            $ID = 0x9204
            $PropertyTagType = $PropertyTagTypeSRational

        Case "FocalLength"
            $ID = 0x920A
            $PropertyTagType = $PropertyTagTypeRational

        Case "ShutterSpeedValue"
            $ID = 0x9201
            $PropertyTagType = $PropertyTagTypeSRational

        Case "ApertureValue"
            $ID = 0x9202
            $PropertyTagType = $PropertyTagTypeRational

        Case "BrightnessValue"
            $ID = 0x9203
            $PropertyTagType = $PropertyTagTypeSRational

        Case "MaxApertureValue"
            $ID = 0x9205
            $PropertyTagType = $PropertyTagTypeRational

        Case "SubjectDistance"
            $ID = 0x9206
            $PropertyTagType = $PropertyTagTypeRational

        Case "UserComments"
            $ID = 0x9286
            $PropertyTagType = $PropertyTagTypeUndefined

        Case Else
            Return SetError(1, -1, False)
    EndSwitch

    ConsoleWrite("$sTagName = " & $sTagName & " | $vStr = " & $vStr & @LF)

    ; Store String In Array:
    $Struct_String = DllStructCreate("char[" & StringLen($vStr) +1 & "];")
    DllStructSetData($Struct_String, 1, $vStr)

    $Struct_Meta = DllstructCreate($tagPropertyItem)
    DllStructSetData($Struct_Meta, "ID", $ID)
    DllStructSetData($Struct_Meta, "Length", StringLen($vStr) +1)
    DllStructSetData($Struct_Meta, "Type", $PropertyTagType)
    DllStructSetData($Struct_Meta, "Value", DllStructGetPtr($Struct_String))

    $aResult = DllCall($ghGDIPDll, "int", "GdipSetPropertyItem", "hwnd", $hHandle, "ptr", DllStructGetPtr($Struct_Meta))
    If @error Then
        Return SetError(@error, @extended, False)
    EndIf
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>_GDIPlus_SetMetaData

SFTPEx, AutoCompleteInput_DateTimeStandard(), _ImageWriteResize()_GUIGraduallyHide(): some AutoIt functions.

Lupo PenSuite: all-in-one and completely free selection of portable programs and games.

DropIt: a personal assistant to automatically manage your files.

ArcThemALL!: application to multi-archive your files and folders.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0