The following example creates an Image object based on a BMP file. The image in that file uses 8 bits per pixel. The code calls the
GdipGetImagePaletteSize function to determine the size of the image's palette. The call to
GdipGetImagePalette fills the
ColorPalette structure. The code displays the number of colors in the palette and then displays the ARGB values of the first five colors in the palette.
C++
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
INT main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Image* image = new Image(L"Stripes8Bit.bmp");
UINT size = image->GetPaletteSize();
printf("The size of the palette is %d bytes.\n", size);
ColorPalette* palette = (ColorPalette*)malloc(size);
image->GetPalette(palette, size);
if(size > 0)
{
printf("There are %u colors in the palette.\n", palette->Count);
printf("The first five colors in the palette are as follows:\n");
for(INT j = 0; j <= 4; ++j)
printf("%x\n", palette->Entries[j]);
}
delete(image);
free(palette);
GdiplusShutdown(gdiplusToken);
return 0;
}
PowerBASIC
#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG
LOCAL hStatus AS LONG
LOCAL token AS DWORD
LOCAL StartupInput AS GdiplusStartupInput
LOCAL pImage AS DWORD
LOCAL strFileName AS STRING
LOCAL nSize AS LONG
LOCAL i AS LONG
LOCAL clrpalette AS COLORPALETTE
' Initialize GDI+
StartupInput.GdiplusVersion = 1
hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
IF hStatus THEN
PRINT "Error initializing GDI+"
EXIT FUNCTION
END IF
' // Create an Image object, and then clone it.
strFileName = UCODE$("climber8bit.bmp")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
hStatus = GdipGetImagePaletteSize(pImage, nSize)
PRINT "The size of the palette is" & STR$(nSize) & " bytes."
IF nSize THEN
hStatus = GdipGetImagePalette(pImage, clrPalette, nSize)
IF hStatus = %StatusOk THEN
PRINT "There are" & STR$(clrPalette.count) & " colors in the palette."
FOR i = 0 TO 4
PRINT HEX$(clrPalette.Entries(i))
NEXT
END IF
END IF
' // Cleanup
IF pImage THEN GdipDisposeImage(pImage)
' Shutdown GDI+
GdiplusShutdown token
WAITKEY$
END FUNCTION
' ========================================================================================