AForge.Imaging.Textures.TextureTools.FromBitmap C# (CSharp) Method

FromBitmap() public static method

Convert grayscale bitmap to texture.
Only grayscale (8 bpp indexed images) are supported.
public static FromBitmap ( UnmanagedImage image ) : ].float[
image UnmanagedImage Image data to convert to texture.
return ].float[
        public static float[,] FromBitmap( UnmanagedImage image )
        {
            // check source image
            if ( image.PixelFormat != PixelFormat.Format8bppIndexed )
                throw new UnsupportedImageFormatException( "Only grayscale (8 bpp indexed images) are supported." );

            // get source image dimension
            int width  = image.Width;
            int height = image.Height;

            // create texture array
            float[,] texture = new float[height, width];

            // do the job
            unsafe
            {
                byte* src = (byte*) image.ImageData.ToPointer( );
                int offset = image.Stride - width;

                // for each line
                for ( int y = 0; y < height; y++ )
                {
                    // for each pixel
                    for ( int x = 0; x < width; x++, src++ )
                    {
                        texture[y, x] = (float) *src / 255.0f;
                    }
                    src += offset;
                }
            }

            return texture;
        }
    }

Same methods

TextureTools::FromBitmap ( Bitmap image ) : ].float[
TextureTools::FromBitmap ( BitmapData imageData ) : ].float[