LEADTOOLS (Leadtools assembly) Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.29
GetPixelData(Int32,Int32,IntPtr,Int32) Method
See Also 
Leadtools Namespace > RasterImage Class > GetPixelData Method : GetPixelData(Int32,Int32,IntPtr,Int32) Method



row
The zero-based row number of the pixel.
column
The zero-based column number of the pixel
data
Pointer to unmanaged memory buffer to copy the pixel data to.
dataSize
Size of bytes the buffer in data.
row
The zero-based row number of the pixel.
column
The zero-based column number of the pixel
data
Pointer to unmanaged memory buffer to copy the pixel data to.
dataSize
Size of bytes the buffer in data.
Copies the pixel data of the specified pixel into an unmanaged memory buffer.

Syntax

Visual Basic (Declaration) 
Overloads Public Sub GetPixelData( _
   ByVal row As Integer, _
   ByVal column As Integer, _
   ByVal data As IntPtr, _
   ByVal dataSize As Integer _
) 
Visual Basic (Usage)Copy Code
Dim instance As RasterImage
Dim row As Integer
Dim column As Integer
Dim data As IntPtr
Dim dataSize As Integer
 
instance.GetPixelData(row, column, data, dataSize)
C# 
public void GetPixelData( 
   int row,
   int column,
   IntPtr data,
   int dataSize
)
C++/CLI 
public:
void GetPixelData( 
   int row,
   int column,
   IntPtr data,
   int dataSize
) 

Parameters

row
The zero-based row number of the pixel.
column
The zero-based column number of the pixel
data
Pointer to unmanaged memory buffer to copy the pixel data to.
dataSize
Size of bytes the buffer in data.

Example

This example uses GetPixelData and SetPixelData(Int32,Int32,Byte[]) methods to swap the R and G values for a particular pixel.

Visual BasicCopy Code
Public Sub GetPixelDataExample()
      Dim codecs As RasterCodecs = New RasterCodecs()
      ' Load the image
      Dim image As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1.CMP"))

      If image.BitsPerPixel = 24 Then
         Dim Data As Byte()
         Dim Value As Byte
         Dim Row As Integer = 10, Column As Integer = 20

         Data = image.GetPixelData(Row, Column)
         ' swap the R and B values
         Value = Data(0)
         Data(0) = Data(2)
         Data(2) = Value

         ' put back the transformed pixel
         image.SetPixelData(Row, Column, Data)
      End If

      codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1_GetPixelData.BMP"), RasterImageFormat.Bmp, 0)

      image.Dispose()
      codecs.Dispose()
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
C#Copy Code
public void GetPixelDataExample()
   {
      RasterCodecs codecs = new RasterCodecs();
      // Load the image
      RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1.CMP"));

      if(image.BitsPerPixel == 24)
      {
         byte[] Data;
         byte Value;
         int Row = 10, Column = 20;

         Data = image.GetPixelData(Row, Column);
         // swap the R and B values
         Value = Data[0]; Data[0] = Data[2]; Data[2] = Value;

         // put back the transformed pixel
         image.SetPixelData(Row, Column, Data);
      }

      codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1_GetPixelData.BMP"), RasterImageFormat.Bmp, 0);

      image.Dispose();
      codecs.Dispose();
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
SilverlightCSharpCopy Code
public void GetPixelDataExample(RasterImage image, Stream destStream)
{
   if (image.BitsPerPixel == 24)
   {
      byte[] Data;
      byte Value;
      int Row = 10, Column = 20;
      Data = image.GetPixelData(Row, Column);
      // swap the R and B values
      Value = Data[0]; Data[0] = Data[2]; Data[2] = Value;

      // put back the transformed pixel
      image.SetPixelData(Row, Column, Data);
   }

   RasterCodecs codecs = new RasterCodecs();
   codecs.Save(image, destStream, RasterImageFormat.Bmp, 0);

   image.Dispose();
}
SilverlightVBCopy Code
Public Sub GetPixelDataExample(ByVal image As RasterImage, ByVal destStream As Stream)
   If image.BitsPerPixel = 24 Then
      Dim Data As Byte()
      Dim Value As Byte
      Dim Row As Integer = 10, Column As Integer = 20
      Data = image.GetPixelData(Row, Column)
      ' swap the R and B values
      Value = Data(0)
      Data(0) = Data(2)
      Data(2) = Value

      ' put back the transformed pixel
      image.SetPixelData(Row, Column, Data)
   End If

   Dim codecs As RasterCodecs = New RasterCodecs()
   codecs.Save(image, destStream, RasterImageFormat.Bmp, 0)

   image.Dispose()
End Sub

Remarks

No transformations are performed on the pixel data.

The pixel data copied may represent an index into an images's palette, a grayscale value ((Document/Medical only), or red, green, and blue color values.

This method should be called only for 8, 16, 24, 32, 48 and 64-bit images. It works as follows:

  • For 8-bit images, this method will retrieve the palette index for the specified pixel.
  • For 16-bit images, this method will retrieve the 2-byte value that forms the pixel.
  • For 24-bit images, this method will retrieve the 3 bytes containing the pixel color. The Order determines whether the data is RGB or BGR.
  • For 32-bit images, this method will retrieve the 4 bytes containing the pixel color and alpha information. The first 3 bytes contain the pixel color. The The Order determines whether the data in the first 3 bytes is RGB or BGR. The 4th byte is the alpha channel information.
  • For 48-bit images, this method will retrieve the 6 bytes containing the pixel color. The Order determines whether the data is RGB or BGR. Each color component is stored as a 16-bit value, where 0 corresponds to black and 0xFFFF corresponds to full white.
  • For 64-bit images, this method will retrieve the 8 bytes containing the pixel color and alpha information. The Order determines whether the data in the first 6 bytes is RGB or BGR. Each color component is stored as a 16-bit value, where 0 corresponds to black and 0xFFFF corresponds to full white. The last two bytes contain the alpha information (also stored as a short value).

The minimum size of the returned byte array is calculated:

(BitsPerPixel + 7) / 8

You can use the SetPixelData(Int32,Int32,IntPtr,Int32) 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.

The SetPixelData(Int32,Int32,IntPtr,Int32) method changes the data of the specified pixel.

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

For more information refer to Accounting for View Perspective.

Requirements

Target Platforms: Silverlight, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7, MAC OS/X (Intel Only), Windows Phone 7

See Also