←Select platform

SetPalette Method

Summary
Sets the palette of this RasterImage object that will be used by 1-8 bit color or grayscale images.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void SetPalette( 
   RasterColor[] palette, 
   int startIndex, 
   int count 
) 
- (BOOL)setPalette:(nullable NSArray<LTRasterColor *> *)palette range:(NSRange)range error:(NSError **)error 
public void setPalette(RasterColor[] palette, int startIndex, int count) 
public: 
void SetPalette(  
   array<RasterColor>^ palette, 
   int startIndex, 
   int count 
)  
def SetPalette(self,] palette,startIndex,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.

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: Palette Handling.

For information on 10 to 16-bit grayscale image, refer to the Grayscale Images.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Core; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Dicom; 
using Leadtools.Drawing; 
using Leadtools.Controls; 
using Leadtools.Svg; 
 
 
public void PaletteExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "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(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.core.*; 
import leadtools.svg.*; 
import leadtools.imageprocessing.CloneCommand; 
import leadtools.imageprocessing.FillCommand; 
import leadtools.imageprocessing.FlipCommand; 
import leadtools.imageprocessing.GrayscaleCommand; 
import leadtools.imageprocessing.color.InvertCommand; 
import leadtools.imageprocessing.color.PosterizeCommand; 
 
 
public void paletteExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1.cmp"); 
   String destFileName = combine(LEAD_VARS_IMAGES_DIR, "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.getBitsPerPixel()); 
 
   // Clean up 
   image.dispose(); 
   codecs.dispose(); 
 
   assertTrue("File unsuccessfully saved to " + srcFileName, (new File(srcFileName)).exists()); 
   System.out.printf("File saved successfully to %s%n", srcFileName); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.