←Select platform

SetPixelColor Method

Summary
Changes the color value of the specified pixel.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void SetPixelColor( 
   int row, 
   int column, 
   RasterColor color 
) 
- (BOOL)setColor:(LTRasterColor *)color  
   forPixelAtRow:(NSInteger)row  
          column:(NSInteger)column  
           error:(NSError **)error 
public void setPixelColor( 
  int row,  
  int column,  
  RasterColor color 
) 
public: 
void SetPixelColor(  
   int row, 
   int column, 
   RasterColor color 
)  
def SetPixelColor(self,row,column,color): 

Parameters

row
The zero-based row number of the pixel.

column
The zero-based column number of the pixel.

color
The color to assign to the pixel.

Remarks

This method uses image coordinates to specify the pixel. Therefore, you must account for the view perspective of the image. If you specify a pixel that is outside the image or outside the region (if the image has one), this method throws an exception.

Use the GetPixelColor method to get the color value of a certain pixel.

This method does not support alpha values for 32 and 64-bit color images and the alpha value of  color is not be used. Instead, use the GetPixel and SetPixel methods.

This method supports unsigned data images, but not signed ones.

The SetPixelColor function can use the Extended Grayscale mask. For more information, refer to Grayscale Images

For more information, refer to Introduction to Image Processing With LEADTOOLS.

For more information refer to Accounting for View Perspective.

In the Document and Medical toolkits, the COLORREF value can represent a 16 bit grayscale value if RasterImage is a 12 or 16-bit grayscale bitmap. To avoid confusion with an RGB value, set the COLORREF_GRAY16 mask (0x04000000). In this case (0x0400YYYY), the lower 16 bits (0xYYYY) of the COLORREF value represent the 16-bit grayscale value. (0x0400FFFF is 16-bit white and 0x04000000 is 16-bit black.) This is not a standard Windows value. Therefore, although LEADTOOLS methods will recognize a COLORREF having this format, but Windows methods will not.

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 GetPixelColorExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
   // Load the image 
   RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1.CMP")); 
 
   // Specify a line of pixels. 
   LeadPoint offset = new LeadPoint(image.Width / 8, image.Height / 8); 
   int XSize = image.Width / 3; 
 
   // Invert the colors of pixels in the line. 
   for (int i = 0; i < XSize; i++) 
   { 
      LeadPoint oldOffset = new LeadPoint(offset.X, offset.Y); 
      // Adjust the XOffset and YOffset  in case the view perspective is not TopLeft. 
      offset = image.PointToImage(Leadtools.RasterViewPerspective.TopLeft, offset); 
      RasterColor pixelColor = image.GetPixelColor(offset.Y, offset.X); 
      pixelColor.R = (byte)(255 - pixelColor.R); 
      pixelColor.G = (byte)(255 - pixelColor.G); 
      pixelColor.B = (byte)(255 - pixelColor.B); 
      image.SetPixelColor(offset.Y, offset.X, pixelColor); 
 
      offset = oldOffset; // Restore Offset 
      offset.X = offset.X + 1; 
   } 
 
   codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1_GetPixelColor.BMP"), RasterImageFormat.Bmp, 0); 
 
   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 getPixelColorExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // Load the image 
   RasterImage image = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "IMAGE1.CMP")); 
   String destFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1_getPixelColor.BMP"); 
 
   // Specify a line of pixels. 
   LeadPoint offset = new LeadPoint(image.getWidth() / 8, image.getHeight() / 8); 
   int XSize = image.getWidth() / 3; 
 
   // Invert the colors of pixels in the line. 
   for (int i = 0; i < XSize; i++) { 
      LeadPoint oldOffset = new LeadPoint(offset.getX(), offset.getY()); 
      // Adjust the XOffset and YOffset in case the view perspective is not TopLeft. 
      offset = image.pointToImage(leadtools.RasterViewPerspective.TOP_LEFT, offset); 
      RasterColor pixelColor = image.getPixelColor(offset.getY(), offset.getX()); 
      pixelColor.r(255 - pixelColor.r()); 
      pixelColor.g(255 - pixelColor.g()); 
      pixelColor.b(255 - pixelColor.b()); 
      image.setPixelColor(offset.getY(), offset.getX(), pixelColor); 
 
      offset = oldOffset; // Restore Offset 
      assertTrue(offset == oldOffset); 
      offset.setX(offset.getX() + 1); 
   } 
 
   // Save it 
   codecs.save(image, destFileName, RasterImageFormat.BMP, 0); 
 
   // Clean up 
   image.dispose(); 
   codecs.dispose(); 
 
   assertTrue("file unsuccessfully saved to " + destFileName, (new File(destFileName)).exists()); 
   System.out.printf("File saved successfully to %s%n", destFileName); 
} 
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.