←Select platform

SetPixel Method

Changes the color value of the specified pixel.
public void SetPixel( 
   int row, 
   int column, 
   RasterColor color 
- (BOOL)setPixel:(LTRasterColor *)color  
           error:(NSError **)error 
public void setPixel( 
  int row,  
  int column,  
  RasterColor color 
void SetPixel(  
   int row, 
   int column, 
   RasterColor color 
def SetPixel(self,row,column,color): 


The zero-based row number of the pixel.

The zero-based column number of the pixel.

The color to assign to the pixel. If the image has 32 or 64-bits per pixel, the alpha value of this parameter is used in the pixel data.


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 GetPixel method to get the color value of a certain pixel.

For 32 and 64-bit images, an alpha value is also available in the image. This method will use the alpha value in  color (A). For 32-bit images, this will be the exact byte value of the alpha for the pixel. For 64-bit images, the alpha is 16-bit and since RasterColor.A is an 8-bit value, the alpha is normalized from its 16-bit value to 8-bit. For other bits/pixel values, the method is identical to returned from SetPixelColor.

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

The SetPixel 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.

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 GetSetPixelExample() 
   string sourceFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp"); 
   string destFile = Path.Combine(LEAD_VARS.ImagesDir, "GetSetPixelExample.png"); 
   RasterCodecs codecs = new RasterCodecs(); 
   // Load the source image as 32-bit (with alpha) 
   RasterImage image = codecs.Load(sourceFile, 32, CodecsLoadByteOrder.Bgr, 1, 1); 
   // Loop through each pixel and set its alpha value to 128 (half transparent) 
   for (int row = 0; row < image.Height; row++) 
      for (int col = 0; col < image.Width; col++) 
         RasterColor color = image.GetPixel(row, col); 
         color.A = 128; 
         image.SetPixel(row, col, color); 
   // Save the image as PNG with these new alpha values 
   codecs.Save(image, destFile, RasterImageFormat.Png, 32); 
   // Clean up 
static class LEAD_VARS 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 

Target Platforms

Help Version 23.0.2024.2.29
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.