Exemple #1
0
        /// <summary>
        /// 分割图片
        /// </summary>
        /// <returns>处理后的验证码</returns>
        public static Bitmap CutImg(Bitmap tempImg)
        {
            Bitmap img = new Bitmap(tempImg);

            parts.Clear();
            //Y轴分割
            CutY(img);
            //区域个数
            int __count = 0;

            if (XList.Count > 1)
            {
                //x起始值
                int __start = XList[0];
                //x结束值
                int __end = XList[XList.Count - 1];
                //x索引
                int __idx = 0;
                while (__start != __end)
                {
                    //区域宽度
                    int __w = __start;
                    //区域个数自加
                    __count++;
                    while (XList.Contains(__w) && __idx < XList.Count)
                    {
                        //区域宽度自加
                        __w++;
                        //x索引自加
                        __idx++;
                    }
                    //区域X轴坐标
                    int x = __start;
                    //区域Y轴坐标
                    int y = 0;
                    //区域宽度
                    int width = __w - __start;
                    //区域高度
                    int height = img.Height;

                    /*
                     * X轴分割当前区域
                     */
                    CutX(img.Clone(new Rectangle(x, y, width, height), img.PixelFormat));
                    if (YList.Count > 1 && YList.Count != img.Height)
                    {
                        int y1 = YList[0];
                        int y2 = YList[YList.Count - 1];
                        if (y1 != 1)
                        {
                            y = y1 - 1;
                        }
                        height = y2 - y1 + 1;
                    }
                    //GDI+绘图对象

                    /*Graphics g = Graphics.FromImage(img);
                     * g.SmoothingMode = SmoothingMode.HighQuality;
                     * g.CompositingMode = CompositingMode.SourceOver;
                     * g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
                     * g.InterpolationMode = InterpolationMode.HighQualityBicubic;*/
                    //画出验证码区域
                    Rectangle r = new Rectangle(x, y + 1, width, height);
                    parts.Add(r);
                    //g.DrawRectangle(new Pen(Brushes.Green), r);
                    // g.Dispose();
                    //起始值指向下一组
                    if (__idx < XList.Count)
                    {
                        __start = XList[__idx];
                    }
                    else
                    {
                        __start = __end;
                    }
                }
            }
            return(img);
        }