AForge.Imaging.UnmanagedImage.SetPixel C# (CSharp) Method

SetPixel() private method

private SetPixel ( int x, int y, byte r, byte g, byte b, byte a ) : void
x int
y int
r byte
g byte
b byte
a byte
return void
        private void SetPixel( int x, int y, byte r, byte g, byte b, byte a )
        {
            if ( ( x >= 0 ) && ( y >= 0 ) && ( x < width ) && ( y < height ) )
            {
                unsafe
                {
                    int pixelSize = Bitmap.GetPixelFormatSize( pixelFormat ) / 8;
                    byte* ptr = (byte*) imageData.ToPointer( ) + y * stride + x * pixelSize;
                    ushort* ptr2 = (ushort*) ptr;

                    switch ( pixelFormat )
                    {
                        case PixelFormat.Format8bppIndexed:
                            *ptr = (byte) ( 0.2125 * r + 0.7154 * g + 0.0721 * b );
                            break;

                        case PixelFormat.Format24bppRgb:
                        case PixelFormat.Format32bppRgb:
                            ptr[RGB.R] = r;
                            ptr[RGB.G] = g;
                            ptr[RGB.B] = b;
                            break;

                        case PixelFormat.Format32bppArgb:
                            ptr[RGB.R] = r;
                            ptr[RGB.G] = g;
                            ptr[RGB.B] = b;
                            ptr[RGB.A] = a;
                            break;

                        case PixelFormat.Format16bppGrayScale:
                            *ptr2 = (ushort) ( (ushort) ( 0.2125 * r + 0.7154 * g + 0.0721 * b ) << 8 );
                            break;

                        case PixelFormat.Format48bppRgb:
                            ptr2[RGB.R] = (ushort) ( r << 8 );
                            ptr2[RGB.G] = (ushort) ( g << 8 );
                            ptr2[RGB.B] = (ushort) ( b << 8 );
                            break;

                        case PixelFormat.Format64bppArgb:
                            ptr2[RGB.R] = (ushort) ( r << 8 );
                            ptr2[RGB.G] = (ushort) ( g << 8 );
                            ptr2[RGB.B] = (ushort) ( b << 8 );
                            ptr2[RGB.A] = (ushort) ( a << 8 );
                            break;

                        default:
                            throw new UnsupportedImageFormatException( "The pixel format is not supported: " + pixelFormat );
                    }
                }
            }
        }

Same methods

UnmanagedImage::SetPixel ( IntPoint point, Color color ) : void
UnmanagedImage::SetPixel ( int x, int y, Color color ) : void
UnmanagedImage::SetPixel ( int x, int y, byte value ) : void

Usage Example

Example #1
0
 public void dessinePoint(IntPoint point, UnmanagedImage img,int nbPixel,Color col)
 {
     for (int i = point.X - nbPixel / 2; i < point.X + nbPixel / 2 + 1; i++)
     {
         for (int j = point.Y - nbPixel / 2; j < point.Y + nbPixel / 2 + 1; j++)
         {
             img.SetPixel(i, j, col);
         }
     }
 }
All Usage Examples Of AForge.Imaging.UnmanagedImage::SetPixel