Пример #1
0
        public static Bitmap BoundCore(Bitmap b)
        {
            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;

            System.IntPtr Scan0 = bmData.Scan0;

            int rw, rn, re, rs;

            rw = rn = re = rs = -1;
            unsafe
            {
                byte *p = (byte *)(void *)Scan0;

                int nOffset = stride - b.Width * 3;

                byte red, green, blue;
                for (int y = 0; y < b.Height; ++y)
                {
                    for (int x = 0; x < b.Width; ++x)
                    {
                        blue  = p[0];
                        green = p[1];
                        red   = p[2];
                        if (blue == 255 && green == 255 && red == 255)
                        {
                            if (rw == -1)
                            {
                                rw = x;
                            }
                            rw = Math.Min(rw, x);
                            if (re == -1)
                            {
                                re = x;
                            }
                            re = Math.Max(re, x);
                            if (rn == -1)
                            {
                                rn = y;
                            }
                            rn = Math.Min(rn, y);
                            if (rs == -1)
                            {
                                rs = y;
                            }
                            rs = Math.Max(rs, y);
                        }
                        p += 3;
                    }
                    p += nOffset;
                }
            }

            b.UnlockBits(bmData);
            int d = Math.Max(re + 1 - rw, rs + 1 - rn);

            return(FontMethods.CropBitmap(b, new Rectangle(rw, rn, d, d)));
        }
Пример #2
0
        public static Bitmap[] ImageToTextR1(Bitmap b)
        {
            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;

            System.IntPtr Scan0 = bmData.Scan0;

            List <Rectangle> brow = new List <Rectangle>();

            unsafe
            {
                byte *p = (byte *)(void *)Scan0;

                int nOffset = stride - b.Width * 3;

                byte red, green, blue;
                int  rx, ry, rw, rh;
                rx = ry = rw = rh = 0;
                bool  fs = false;
                int[] fr = new int[b.Height];
                for (int y = 0; y < b.Height; ++y)
                {
                    for (int x = 0; x < b.Width; ++x)
                    {
                        blue  = p[0];
                        green = p[1];
                        red   = p[2];
                        if (blue == 0 && green == 0 && red == 0)
                        {
                            fr[y]++;
                        }
                        p += 3;
                    }
                    p += nOffset;
                }
                for (int y = 0; y < b.Height; ++y)
                {
                    //first possition has byte -> start row
                    if ((!fs) && fr[y] < b.Width)
                    {
                        rx = 0;
                        ry = y;
                        rw = b.Width;
                        fs = true;
                    }
                    else
                    {
                        //fisrt possition no byte after has byte -> end row
                        if (fs && ((fr[y] == b.Width) || (y + 1 == b.Height)))
                        {
                            rh = y - ry + 1;
                            Rectangle bm = new Rectangle(rx, ry, rw, rh);
                            brow.Add(bm);
                            if (brow.Count > 1)
                            {
                                Rectangle f = brow[brow.Count - 1];
                                Rectangle l = brow[brow.Count - 2];
                                //minimum distance between row 4px
                                if (f.Y + 1 - l.Y - l.Height - 4 < 0)
                                {
                                    Rectangle n = new Rectangle(l.X, l.Y, f.Width, l.Height + f.Y + 1 - l.Y);
                                    brow.RemoveRange(brow.Count - 2, 2);
                                    brow.Add(n);
                                }
                            }
                            rx = ry = rw = rh = 0;
                            fs = false;
                        }
                    }
                }
            }

            b.UnlockBits(bmData);

            Bitmap[] ar = FontMethods.CropBitmap(b, brow.ToArray());
            return(ar);
        }