示例#1
0
        public Rectangle DetectEdges(Bitmap source)
        {
            var fastSource = new FastBitmap(source);

            fastSource.LockImage();

            var(X, Width)  = DetectVerticalLines(fastSource);
            var(Y, Height) = DetectHorizontalLines(fastSource);

            fastSource.UnlockImage();

            if (Width > 0 && Height > 0)
            {
                Width  -= 8;
                X      += 4;
                Height -= 8;
                Y      += 4;
                var rectangle = new Rectangle(X, Y, Width, Height);
                RectangleDetected?.Invoke(this, new RectangleEventArgs(rectangle));
                return(rectangle);
            }
            return(Rectangle.Empty);
        }
示例#2
0
        Bitmap Crop(Bitmap source, Rectangle rectangle)
        {
            var fastSource = new FastBitmap(source);

            var croppedImage     = new Bitmap(rectangle.Width, rectangle.Height);
            var fastCroppedImage = new FastBitmap(croppedImage);

            fastCroppedImage.LockImage();
            fastSource.LockImage();

            for (int y = rectangle.Y, startY = rectangle.Y, endY = rectangle.Y + rectangle.Height; y < endY; y++)
            {
                for (int x = rectangle.X, startX = rectangle.X, endX = rectangle.X + rectangle.Width; x < endX; x++)
                {
                    var pixel = fastSource.GetPixel(x, y);
                    fastCroppedImage.SetPixel(x - startX, y - startY, pixel);
                }
            }

            fastCroppedImage.UnlockImage();
            fastSource.UnlockImage();
            return(croppedImage);
        }