Help Center

CGpImage.SetPalettemethod

Sets the color palette of the Image object.

GraphicsmethodCGpBitmap.incdocumented

Syntax

FUNCTION SetPalette (BYVAL pal AS ColorPalette PTR) AS GpStatus

Parameters

NameDescription
palettePointer to a ColorPalette structure that specifies the palette.

Return value

If the function succeeds, it returns StatusOk, which is an element of the GpStatus enumeration.

If the function fails, it returns one of the other elements of the GpStatus enumeration.

Description

Sets the color palette of an Image object.

Example

' ======================================================================================== ' The SetImagePalette method sets a custom color palette for an image object in GDI+. ' This only applies to indexed pixel formats like 1bpp, 4bpp, or 8bpp images (e.g., GIFs or 8-bit BMPs). ' It replaces the image’s current palette with a new one you define. ' The palette must match the image’s pixel format (e.g., 256 colors for 8bpp). ' Useful for customizing color mapping, reducing color depth, or preparing images ' for formats that require indexed color. ' If you try to use it on a 24-bit or 32-bit image, it won’t work—those formats don’t use palettes. ' ========================================================================================

' Create a bitmap with 8bpp indexed format DIM nWidth AS LONG = 64 DIM nHeight AS LONG = 64 DIM bmp AS CGpBitmap = CGpBitmap(nWidth, nHeight, 0, PixelFormat8bppIndexed, NULL) IF bmp.GetLastStatus <> StatusOk THEN PRINT "Failed to create indexed bitmap" : END

' // Allocate and define a palette DIM paletteSize AS LONG = SIZEOF(ColorPalette) + (256 * SIZEOF(ARGB)) DIM paletteBuffer AS UBYTE PTR = Allocate(paletteSize) DIM paletteptr AS ColorPalette PTR = cast(ColorPalette PTR, paletteBuffer) paletteptr->Flags = 0 paletteptr->Count = 256 FOR i AS LONG = 0 To 255 paletteptr->Entries(i) = &HFF000000 OR (i SHL 16) ' Shades of red NEXT

' // Set the palette bmp.SetPalette(paletteptr) IF bmp.GetLastStatus = StatusOk THEN PRINT "Palette set successfully" ELSE PRINT "Failed to set palette. Status = "; bmp.GetLastStatus END IF

' // Cleanup IF paletteBuffer THEN Deallocate(paletteBuffer)

PRINT PRINT "Press any key" SLEEP

Reference

  • Include file CGpBitmap.inc
  • Defined in AfxNova/CGpBitmap.inc:257
  • Documented in Graphics/GdiPlus Classes/CGpImage Classes.md
  • Topic: CGpImage Class