Aspose.Plugins.AsposeVSOpenXML.Program.FindBoundingBoxAroundNode C# (CSharp) Method

FindBoundingBoxAroundNode() public static method

Finds the minimum bounding box around non-transparent pixels in a Bitmap.
public static FindBoundingBoxAroundNode ( Bitmap originalBitmap ) : Rectangle
originalBitmap Bitmap
return Rectangle
        public static Rectangle FindBoundingBoxAroundNode(Bitmap originalBitmap)
        {
            Point min = new Point(int.MaxValue, int.MaxValue);
            Point max = new Point(int.MinValue, int.MinValue);

            for (int x = 0; x < originalBitmap.Width; ++x)
            {
                for (int y = 0; y < originalBitmap.Height; ++y)
                {
                    // Note that you can speed up this part of the algorithm by using LockBits and unsafe code instead of GetPixel.
                    Color pixelColor = originalBitmap.GetPixel(x, y);

                    // For each pixel that is not transparent calculate the bounding box around it.
                    if (pixelColor.ToArgb() != Color.Empty.ToArgb())
                    {
                        min.X = Math.Min(x, min.X);
                        min.Y = Math.Min(y, min.Y);
                        max.X = Math.Max(x, max.X);
                        max.Y = Math.Max(y, max.Y);
                    }
                }
            }

            // Add one pixel to the width and height to avoid clipping.
            return new Rectangle(min.X, min.Y, (max.X - min.X) + 1, (max.Y - min.Y) + 1);
        }
        //ExEnd