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



row
The zero-based row number of the pixel.
column
The zero-based column number of the pixel.
Returns the color of the specified pixel.

Syntax

Visual Basic (Declaration)  
Public Function GetPixelColor( _
   ByVal row As Integer, _
   ByVal column As Integer _
) As RasterColor
Visual Basic (Usage) Copy Code
Dim instance As RasterImage
Dim row As Integer
Dim column As Integer
Dim value As RasterColor
 
value = instance.GetPixelColor(row, column)
C#  
public RasterColor GetPixelColor( 
   int row,
   int column
)
C++/CLI  
public:
RasterColor GetPixelColor( 
   int row,
   int column
) 

Parameters

row
The zero-based row number of the pixel.
column
The zero-based column number of the pixel.

Return Value

This method returns a RasterColor value which may represent an index into a image palette, a grayscale value (Medical Imaging only), or red, green, and blue color values.

Example

This example uses GetPixelColor and SetPixelColor to invert the colors of the pixels in a line in the upper left part of the displayed image.

Visual Basic Copy Code
Public Sub GetPixelColorExample()
   RasterCodecs.Startup()
   Dim codecs As RasterCodecs = New RasterCodecs()
   ' Load the image
   Dim image As RasterImage = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1.CMP")

   ' Specify a line of pixels.
   Dim Offset As Point = New Point(image.Width \ 8, image.Height \ 8)
   Dim XSize As Integer = image.Width \ 3

   ' Invert the colors of pixels in the line.
   Dim i As Integer = 0
   Do While i < XSize
      Dim OldOffset As Point = New Point(Offset.X, Offset.Y)
      ' Adjust the XOffset and YOffset in case the view perspective is not TopLeft.
      Offset = image.PointToImage(RasterViewPerspective.TopLeft, Offset)
      Dim PixelColor As RasterColor = image.GetPixelColor(Offset.Y, Offset.X)
      PixelColor.R = CByte(255 - PixelColor.R)
      PixelColor.G = CByte(255 - PixelColor.G)
      PixelColor.B = CByte(255 - PixelColor.B)
      image.SetPixelColor(Offset.Y, Offset.X, PixelColor)

      Offset = OldOffset ' Restore Offset
      Offset.X = Offset.X + 1
      i += 1
   Loop

   codecs.Save(image, LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1_GetPixelColor.BMP", RasterImageFormat.Bmp, 0)

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

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
   // Load the image 
   RasterImage image = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1.CMP"); 
 
   // Specify a line of pixels. 
   Point Offset = new Point(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++) 
   { 
      Point OldOffset = new Point(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, LeadtoolsExamples.Common.ImagesPath.Path + "IMAGE1_GetPixelColor.BMP", RasterImageFormat.Bmp, 0); 
 
   image.Dispose(); 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 
}

Remarks

The standard Windows values for RasterColor represent either red, green, and blue color values, or an index into the image palette. A RasterColor value with the RGB value of 0x00BBGGRR represents the blue, green, and red color values for the specified pixel, where 0xBB is the blue value, 0xGG is the green value and 0xRR is the red value. If 0x01000000 is set in the RGB value of RasterColor (0x010000ZZ), the lower 8 bits (0xZZ) represent an index into the image palette which holds the color value.

You can use the SetPixelColor method to assign the returned value to another pixel.

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.

This method does not support signed images.

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

For more information refer to Accounting for View Perspective.

Requirements

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

See Also