Exemple #1
0
        // Static method
        public static ByteImage convertBitmapToByteImage(Bitmap bitmapImage)
        {
            BitmapData bdInputImage = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            byte PixelSize = 3; // Red Green Blue

            // set array segment
            ByteImage byImage = new ByteImage(bitmapImage.Width, bitmapImage.Height);

            unsafe
            {
                byte* pInputImage = (byte*)(void*)bdInputImage.Scan0;
                // details explanation about stride image, http://www.codersource.net/csharp_image_Processing.aspx
                int nOffset = bdInputImage.Stride - bdInputImage.Width * PixelSize;
                for (int y = 0; y < bdInputImage.Height; y++)
                {
                    for (int x = 0; x < bdInputImage.Width; x++)
                    {
                        byImage.SetAll(x, y, pInputImage[2], pInputImage[1], pInputImage[0]);
                        pInputImage += PixelSize;
                    }
                    pInputImage += nOffset;
                }
            }

            bitmapImage.UnlockBits(bdInputImage);
            return byImage;
        }