The following console application creates a
Image object based on a JPEG file. The code calls the
GdipSetPropertyItem function of that Image object to set the title of the image. Then the code retrieves and displays the new title.
C++
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
INT main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Create an Image object based on a JPEG file.
Image* image = new Image(L"FakePhoto.jpg");
// Set the image title.
PropertyItem* propItem = new PropertyItem;
CHAR newTitleValue[] = "Fake Photograph 2";
propItem->id = PropertyTagImageTitle;
propItem->length = 18; // includes null terminator
propItem->type = PropertyTagTypeASCII;
propItem->value = newTitleValue;
image->SetPropertyItem(propItem);
// Get and display the new image title.
UINT size = image->GetPropertyItemSize(PropertyTagImageTitle);
PropertyItem* title = (PropertyItem*)malloc(size);
image->GetPropertyItem(PropertyTagImageTitle, size, title);
printf("The image title is %s.\n", title->value);
free(title);
delete propItem;
delete image;
GdiplusShutdown(gdiplusToken);
return 0;
}
PowerBASIC
#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"
#INCLUDE "GDIPUTILS.INC"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL hStatus AS LONG
LOCAL token AS DWORD
LOCAL StartupInput AS GdiplusStartupInput
LOCAL pImage AS DWORD
LOCAL strFileName AS STRING
LOCAL propItem AS PropertyItem
LOCAL szNewTitleValue AS ASCIIZ * 256
LOCAL nSize AS DWORD
LOCAL buffer AS STRING
LOCAL pvAsciiz AS ASCIIZ PTR
LOCAL EncoderClsid AS GUID
' // Initialize GDI+
StartupInput.GdiplusVersion = 1
hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
IF hStatus THEN
PRINT "Error initializing GDI+"
EXIT FUNCTION
END IF
strFileName = UCODE$("BERRY_Halle.jpg")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
szNewTitleValue = "Halle Berry"
propItem.id = %PropertyTagImageTitle
propItem.length = LEN(szNewTitleValue) + 1 ' // Includes null terminator
propItem.type = %PropertyTagTypeASCII
propItem.value = VARPTR(szNewTitleValue)
hStatus = GdipSetPropertyItem(pImage, propItem)
' // GEt and display the new image title
RESET propItem
hStatus = GdipGetPropertyItemSize(pImage, %PropertyTagImageTitle, nSize)
buffer = SPACE$(nSize)
hStatus = GdipGetPropertyItem(pImage, %PropertyTagImageTitle, nSize, BYVAL STRPTR(buffer))
LSET propItem = buffer
IF propItem.value THEN
pvAsciiz = propItem.value
PRINT "New title: " & @pvAsciiz
END IF
' // Get the CLSID of the PNG encoder.
EncoderClsid = GUID$(GdiPlusGetEncoderClsid("image/jpeg"))
' // Save the image with another name.
strFileName = UCODE$("BERRY_Halle_Modified.jpg")
hStatus = GdipSaveImageToFile(pImage, STRPTR(strFileName), EncoderClsid, BYVAL %NULL)
IF hStatus = %StatusOk THEN
PRINT "BERRY_Halle_Modified.jpg saved successfully"
ELSE
PRINT "Attempt to save BERRY_Halle_Modified.jpg failed"
END IF
' // Cleanup
IF pImage THEN GdipDisposeImage(pImage)
' // Shutdown GDI+
GdiplusShutdown token
WAITKEY$
END FUNCTION
' ========================================================================================
José,
This MSDN code won't work with the BERRY_Halle.jpg file provided in the zip file.
This will report the GDIPLUS error 19 (GpStatusPropertyNotFound) trying to read the PropertyTagImageTitle from the resulting "BERRY_Halle_Modified.jpg" file.
Rather use the PropertyTagImageDescription
See the details here:
http://msdn.microsoft.com/en-us/library/ms534416(v=VS.85).aspx#_gdiplus_constant_propertytagimagedescription
To fix the code, change all occurences of PropertyTagImageTitle into PropertyTagImageDescription, and see the result within the Windows Explorer:
Right click on the "BERRY_Halle_Modified.jpg" file name,
then select properties,
the select the "Details" tab.
On Windows Seven, you should see in the Description section:
Title Halle Berry
Object Halle Berry
...