public ConnectedComponent(ConnectedComponent _cc)
 {
     iniParameter(_cc.Left_Top, _cc.Right_Bottom, _cc.PointSet);
 }
        /// <summary>
        /// According to the point information to find the left-top and right-bottom point.
        /// </summary>
        /// <param name="currentCC"></param>
        private static void FormatConnectedComponent(ConnectedComponent currentCC)
        {
            int minWidth = int.MaxValue;
            int maxWidth = int.MinValue;
            int minHeight = int.MaxValue;
            int maxHeight = int.MinValue;

            foreach (iPoint point in currentCC.PointSet)
            {
                if (point.Width_Position < minWidth)
                {
                    minWidth = point.Width_Position;
                }
                if (point.Width_Position > maxWidth)
                {
                    maxWidth = point.Width_Position;
                }
                if (point.Height_Position < minHeight)
                {
                    minHeight = point.Height_Position;
                }
                if (point.Height_Position > maxHeight)
                {
                    maxHeight = point.Height_Position;
                }
            }

            currentCC.Left_Top = new iPoint(minWidth, minHeight);
            currentCC.Right_Bottom = new iPoint(maxWidth, maxHeight);
        }
        /// <summary>
        /// Find all connected-components in the image and return related information.
        /// </summary>
        /// <param name="sourceImage"></param>
        /// <returns></returns>
        public static List<ConnectedComponent> findAllConnectedComponents(Bitmap sourceImage)
        {
            int i = 0, j = 0;
            int tw = 0, th = 0;

            int width = sourceImage.Width;
            int height = sourceImage.Height;

            // The array which tells whether the point at position[i,j] has been visited.
            bool[,] visited = new bool[width, height];

            List<ConnectedComponent> ccResult = new List<ConnectedComponent>();

            try
            {
                for (i = 0; i < width; i++)
                {
                    for (j = 0; j < height; j++)
                    {
                        if (judgeWhitePoint(sourceImage.GetPixel(i, j)) && !visited[i, j])
                        {
                            visited[i, j] = true;

                            ConnectedComponent currentCC = new ConnectedComponent();

                            currentCC.PointSet.Add(new iPoint(i, j));
                            try
                            {
                                BFS(i, j, ref visited, currentCC, sourceImage);
                            }
                            catch (Exception ea)
                            {
                                throw new Exception(ea.Message);
                            }

                            FormatConnectedComponent(currentCC);

                            tw = currentCC.Right_Bottom.Width_Position - currentCC.Left_Top.Width_Position;
                            th = currentCC.Right_Bottom.Height_Position - currentCC.Left_Top.Height_Position;

                            if (tw < width / 4 && (tw >= Config.CC_Size_Threshold || th >= Config.CC_Size_Threshold))
                            {
                                ccResult.Add(currentCC);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return ccResult;
        }
        /// <summary>
        /// Write one connected component to certain bmp image.
        /// </summary>
        /// <param name="ccImage"></param>
        /// <param name="sourceImage"></param>
        public static Bitmap SaveConnectedComponentAsBmp(ConnectedComponent ccImage, Bitmap sourceImage)
        {
            int width = ccImage.Right_Bottom.Width_Position - ccImage.Left_Top.Width_Position;
            int height = ccImage.Right_Bottom.Height_Position - ccImage.Left_Top.Height_Position;

            Bitmap ccBmp = new Bitmap(width + 1, height + 1, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Graphics ccGraphics = Graphics.FromImage(ccBmp);

            // Set the background color as black.
            ccGraphics.Clear(Color.FromArgb(0, 0, 0));

            ccGraphics.Dispose();

            foreach (iPoint point in ccImage.PointSet)
            {
                ccBmp.SetPixel(point.Width_Position - ccImage.Left_Top.Width_Position,
                               point.Height_Position - ccImage.Left_Top.Height_Position,
                               sourceImage.GetPixel(point.Width_Position, point.Height_Position)
                );
            }

            return ccBmp;
        }
        /// <summary>
        /// Using breadth first search to find all white connected components.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="visited"></param>
        /// <param name="ccResult"></param>
        public static void BFS(int width, int height, ref bool[,] visited, ConnectedComponent ccResult, Bitmap sourceImage)
        {
            int i = 0;
            int len = 4;
            int tx = 0, ty = 0;

            int[] dirX = new int[] { 0, 1, 0, -1 };
            int[] dirY = new int[] { 1, 0, -1, 0 };

            visited[width, height] = true;

            try
            {
                Queue<iPoint> pointQueue = new Queue<iPoint>();
                pointQueue.Enqueue(new iPoint(width, height));
                while (pointQueue.Count > 0)
                {
                    iPoint tmp = pointQueue.Dequeue();
                    ccResult.PointSet.Add(new iPoint(tmp.Width_Position, tmp.Height_Position));
                    for (i = 0; i < len; i++)
                    {
                        tx = tmp.Width_Position + dirX[i];
                        ty = tmp.Height_Position + dirY[i];
                        if (tx >= 0 && tx < sourceImage.Width &&
                            ty >= 0 && ty < sourceImage.Height &&
                            !visited[tx, ty] &&
                            judgeWhitePoint(sourceImage.GetPixel(tx, ty)))
                        {
                            visited[tx, ty] = true;
                            pointQueue.Enqueue(new iPoint(tx, ty));
                        }
                    }
                }
            }
            catch (Exception ecc)
            {
                throw new Exception(ecc.Message);
            }
        }