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

SetPixels() public method

Set pixels with the specified coordinates to the specified color.

For images having 16 bpp per color plane, the method extends the specified color value to 16 bit by multiplying it by 256.

public SetPixels ( List coordinates, Color color ) : void
coordinates List List of points to set color for.
color Color Color to set for the specified points.
return void
        public void SetPixels( List<IntPoint> coordinates, Color color )
        {
            unsafe
            {
                int pixelSize = Bitmap.GetPixelFormatSize( pixelFormat ) / 8;
                byte* basePtr = (byte*) imageData.ToPointer( );

                byte red   = color.R;
                byte green = color.G;
                byte blue  = color.B;
                byte alpha = color.A;

                switch ( pixelFormat )
                {
                    case PixelFormat.Format8bppIndexed:
                        {
                            byte grayValue = (byte) ( 0.2125 * red + 0.7154 * green + 0.0721 * blue );

                            foreach ( IntPoint point in coordinates )
                            {
                                if ( ( point.X >= 0 ) && ( point.Y >= 0 ) && ( point.X < width ) && ( point.Y < height ) )
                                {
                                    byte* ptr = basePtr + point.Y * stride + point.X;
                                    *ptr = grayValue;
                                }
                            }
                        }
                        break;

                    case PixelFormat.Format24bppRgb:
                    case PixelFormat.Format32bppRgb:
                        {


                            foreach ( IntPoint point in coordinates )
                            {
                                if ( ( point.X >= 0 ) && ( point.Y >= 0 ) && ( point.X < width ) && ( point.Y < height ) )
                                {
                                    byte* ptr = basePtr + point.Y * stride + point.X * pixelSize;
                                    ptr[RGB.R] = red;
                                    ptr[RGB.G] = green;
                                    ptr[RGB.B] = blue;
                                }
                            }
                        }
                        break;

                    case PixelFormat.Format32bppArgb:
                        {
                            foreach ( IntPoint point in coordinates )
                            {
                                if ( ( point.X >= 0 ) && ( point.Y >= 0 ) && ( point.X < width ) && ( point.Y < height ) )
                                {
                                    byte* ptr = basePtr + point.Y * stride + point.X * pixelSize;
                                    ptr[RGB.R] = red;
                                    ptr[RGB.G] = green;
                                    ptr[RGB.B] = blue;
                                    ptr[RGB.A] = alpha;
                                }
                            }
                        }
                        break;

                    case PixelFormat.Format16bppGrayScale:
                        {
                            ushort grayValue = (ushort) ( (ushort) ( 0.2125 * red + 0.7154 * green + 0.0721 * blue ) << 8 );

                            foreach ( IntPoint point in coordinates )
                            {
                                if ( ( point.X >= 0 ) && ( point.Y >= 0 ) && ( point.X < width ) && ( point.Y < height ) )
                                {
                                    ushort* ptr = (ushort*) ( basePtr + point.Y * stride ) + point.X;
                                    *ptr = grayValue;
                                }
                            }
                        }
                        break;

                    case PixelFormat.Format48bppRgb:
                        {
                            ushort red16   = (ushort) ( red   << 8 );
                            ushort green16 = (ushort) ( green << 8 );
                            ushort blue16  = (ushort) ( blue  << 8 );

                            foreach ( IntPoint point in coordinates )
                            {
                                if ( ( point.X >= 0 ) && ( point.Y >= 0 ) && ( point.X < width ) && ( point.Y < height ) )
                                {
                                    ushort* ptr = (ushort*) ( basePtr + point.Y * stride + point.X * pixelSize );
                                    ptr[RGB.R] = red16;
                                    ptr[RGB.G] = green16;
                                    ptr[RGB.B] = blue16;
                                }
                            }
                        }
                        break;

                    case PixelFormat.Format64bppArgb:
                        {
                            ushort red16   = (ushort) ( red   << 8 );
                            ushort green16 = (ushort) ( green << 8 );
                            ushort blue16  = (ushort) ( blue  << 8 );
                            ushort alpha16 = (ushort) ( alpha << 8 );

                            foreach ( IntPoint point in coordinates )
                            {
                                if ( ( point.X >= 0 ) && ( point.Y >= 0 ) && ( point.X < width ) && ( point.Y < height ) )
                                {
                                    ushort* ptr = (ushort*) ( basePtr + point.Y * stride + point.X * pixelSize );
                                    ptr[RGB.R] = red16;
                                    ptr[RGB.G] = green16;
                                    ptr[RGB.B] = blue16;
                                    ptr[RGB.A] = alpha16;
                                }
                            }
                        }
                        break;

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