public Color GetPixel( int x, int y )
{
if ( ( x < 0 ) || ( y < 0 ) )
{
throw new ArgumentOutOfRangeException( "x", "The specified pixel coordinate is out of image's bounds." );
}
if ( ( x >= width ) || ( y >= height ) )
{
throw new ArgumentOutOfRangeException( "y", "The specified pixel coordinate is out of image's bounds." );
}
Color color = new Color( );
unsafe
{
int pixelSize = Bitmap.GetPixelFormatSize( pixelFormat ) / 8;
byte* ptr = (byte*) imageData.ToPointer( ) + y * stride + x * pixelSize;
switch ( pixelFormat )
{
case PixelFormat.Format8bppIndexed:
color = Color.FromArgb( *ptr, *ptr, *ptr );
break;
case PixelFormat.Format24bppRgb:
case PixelFormat.Format32bppRgb:
color = Color.FromArgb( ptr[RGB.R], ptr[RGB.G], ptr[RGB.B] );
break;
case PixelFormat.Format32bppArgb:
color = Color.FromArgb( ptr[RGB.A], ptr[RGB.R], ptr[RGB.G], ptr[RGB.B] );
break;
default:
throw new UnsupportedImageFormatException( "The pixel format is not supported: " + pixelFormat );
}
}
return color;
}