Exemple #1
0
        public static List <pixel> FindGridForCentrePixel(List <pixel> source, pixel centrePixel, Bitmap image, int currentIndex)
        {
            List <pixel> PixelGrid = new List <pixel>();
            int          rowEnd    = image.Width - 1;

            pixel X00 = (currentIndex - rowEnd) - 1 > -1 ? source[(currentIndex - rowEnd) - 1]: source[currentIndex];
            pixel X01 = (currentIndex - rowEnd) > -1 ? source[(currentIndex - rowEnd)] : source[currentIndex];
            pixel X02 = (currentIndex - rowEnd) + 1 > -1 ? source[(currentIndex - rowEnd) + 1] : source[currentIndex];

            pixel X10 = currentIndex - 1 > -1? source[currentIndex - 1]: source[currentIndex];
            pixel X11 = source[currentIndex];
            pixel X12 = currentIndex + 1 < source.Count - 1 ? source[currentIndex + 1] : source[currentIndex];

            pixel X20 = (currentIndex + rowEnd) - 1 < source.Count - 1? source[(currentIndex + rowEnd) - 1] : source[currentIndex];
            pixel X21 = (currentIndex + rowEnd) < source.Count - 1 ? source[(currentIndex + rowEnd)] : source[currentIndex];
            pixel X22 = (currentIndex + rowEnd) + 1 < source.Count - 1 ? source[(currentIndex + rowEnd) + 1] : source[currentIndex];

            PixelGrid.Add(X00);
            PixelGrid.Add(X01);
            PixelGrid.Add(X02);
            PixelGrid.Add(X10);
            PixelGrid.Add(X11);
            PixelGrid.Add(X12);
            PixelGrid.Add(X20);
            PixelGrid.Add(X21);
            PixelGrid.Add(X22);
            return(PixelGrid);
        }
        public static List <pixel> GetPixelListFromBitmap(Bitmap sourceImage)
        {
            BitmapData sourceData = sourceImage.LockBits(new Rectangle(0, 0,
                                                                       sourceImage.Width, sourceImage.Height),
                                                         ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            byte[] sourceBuffer = new byte[sourceData.Stride * sourceData.Height];
            Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, sourceBuffer.Length);
            sourceImage.UnlockBits(sourceData);

            List <pixel> pixelList = new List <pixel>();

            int x = 0;
            int y = 0;

            using (MemoryStream memoryStream = new MemoryStream(sourceBuffer))
            {
                memoryStream.Position = 0;
                BinaryReader binaryReader = new BinaryReader(memoryStream);

                while (memoryStream.Position + 4 <= memoryStream.Length)
                {
                    pixel pixel = new pixel(binaryReader.ReadBytes(4), x, y);
                    pixelList.Add(pixel);

                    x += 1;

                    if (x >= sourceData.Width) //if x length is greater than image width...
                    {
                        x  = 0;                //reset x to 0
                        y += 1;                //create a new line (y value)
                    }
                }

                binaryReader.Close();
            }

            return(pixelList);
        }