Leadtools Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
SetPalette Method
See Also  Example
Leadtools Namespace > RasterImage Class : SetPalette Method



palette
An array of RasterColor containing the new palette to use.
startIndex
Index of the first palette entry to replace.
count
Number of palette entries to replace.
Sets the palette of this RasterImage object.

Syntax

Visual Basic (Declaration) 
Public Sub SetPalette( _
   ByVal palette() As RasterColor, _
   ByVal startIndex As Integer, _
   ByVal count As Integer _
) 
Visual Basic (Usage)Copy Code
Dim instance As RasterImage
Dim palette() As RasterColor
Dim startIndex As Integer
Dim count As Integer
 
instance.SetPalette(palette, startIndex, count)
C# 
public void SetPalette( 
   RasterColor[] palette,
   int startIndex,
   int count
)
C++/CLI 
public:
void SetPalette( 
   array<RasterColor>^ palette,
   int startIndex,
   int count
) 

Parameters

palette
An array of RasterColor containing the new palette to use.
startIndex
Index of the first palette entry to replace.
count
Number of palette entries to replace.

Example

Visual BasicCopy Code
Public Sub PaletteExample()
   RasterCodecs.Startup()
   Dim codecs As RasterCodecs = New RasterCodecs()

   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
   Dim destFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_RedPalette.bmp"

   ' Load the image as 8 bits/pixel
   Dim image As RasterImage = codecs.Load(srcFileName, 8, CodecsLoadByteOrder.Rgb, 1, 1)

   ' Get the palette of this image and change it to shades of red
   Dim palette As RasterColor() = image.GetPalette()
   Dim i As Integer = 0
   Do While i < palette.Length
      palette(i) = New RasterColor(i, 0, 0)
      i += 1
   Loop

   ' Set the palette back in the image then save the file
   image.SetPalette(palette, 0, palette.Length)
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, image.BitsPerPixel)

   image.Dispose()

   codecs.Dispose()
   RasterCodecs.Shutdown()
End Sub
C#Copy Code
public void PaletteExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
   string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_RedPalette.bmp"; 
 
   // Load the image as 8 bits/pixel 
   RasterImage image = codecs.Load(srcFileName, 8, CodecsLoadByteOrder.Rgb, 1, 1); 
 
   // Get the palette of this image and change it to shades of red 
   RasterColor[] palette = image.GetPalette(); 
   for(int i = 0; i < palette.Length; i++) 
      palette[i] = new RasterColor(i, 0, 0); 
 
   // Set the palette back in the image then save the file 
   image.SetPalette(palette, 0, palette.Length); 
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, image.BitsPerPixel); 
 
   image.Dispose(); 
 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 
}

Remarks

The image data of a RasterImage object with BitsPerPixel values of 1, 2, 3, 4, 5, 6, 7 or 8 consists of an index into the image palette. You can get a copy of the palette used by using the GetPalette method.

You can change the palette used by the RasterImage object with the SetPalette method.

This method does not support signed images.

For more information, refer to RasterPalette and Introduction.

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also