Exemple #1
0
        public static Rectangle GetNonTransparentRect(IBitmap bmp)
        {
            int w = bmp.Width;
            int h = bmp.Height;

            int topmost = 0;

            for (int row = 0; row < h; ++row)
            {
                if (!bmp.EmptyRow(row))
                {
                    break;
                }
                topmost = row;
            }

            int bottommost = 0;

            for (int row = h - 1; row >= 0; --row)
            {
                if (!bmp.EmptyRow(row))
                {
                    break;
                }
                bottommost = row;
            }

            int leftmost = 0, rightmost = 0;

            for (int col = 0; col < w; ++col)
            {
                if (!bmp.EmptyColumn(col))
                {
                    break;
                }
                leftmost = col;
            }

            for (int col = w - 1; col >= 0; --col)
            {
                if (!bmp.EmptyColumn(col))
                {
                    break;
                }
                rightmost = col;
            }

            if (rightmost == 0)
            {
                rightmost = w;                 // As reached left
            }
            if (bottommost == 0)
            {
                bottommost = h;                  // As reached top.
            }
            int croppedWidth  = rightmost - leftmost;
            int croppedHeight = bottommost - topmost;

            if (croppedWidth == 0) // No border on left or right
            {
                leftmost     = 0;
                croppedWidth = w;
            }

            if (croppedHeight == 0) // No border on top or bottom
            {
                topmost       = 0;
                croppedHeight = h;
            }

            try
            {
                return(new Rectangle(leftmost, topmost, croppedWidth, croppedHeight));
            }
            catch (Exception ex)
            {
                throw new Exception(
                          string.Format("Values are topmost={0} btm={1} left={2} right={3} croppedWidth={4} croppedHeight={5}", topmost, bottommost, leftmost, rightmost, croppedWidth, croppedHeight),
                          ex);
            }
        }