private static RectCollection buildBoxes(Bitmap bitmap) { List<CRectangle> rt = new List<CRectangle>(); UnsafeBitmap bmp = new UnsafeBitmap(bitmap); bmp.LockBitmap(); var gapHeight = 40; for (int y = 0; y < bitmap.Height; y++) { List<CRectangle> rtc = new List<CRectangle>(); foreach (var r in rt) { if (r.Y + r.Height + gapHeight + 1 >= y) { rtc.Add(r); } } for (int x = 0; x < bitmap.Width; x++) { var pix = bmp.GetPixel(x, y); if (!(pix.red == 255 && pix.green == 255 && pix.blue == 255)) { List<CRectangle> ins = new List<CRectangle>(); if (rtc.Count > 1) { } foreach (var rectangle in rtc) { if (rectangle.ShouldContain(x, y)) { if (rectangle.Contains(x, y)) { ins.Add(rectangle); // Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} contians"); } else if (rectangle.Contains(x, y - 1)) { ins.Add(rectangle); rectangle.Height = rectangle.Height + 1; // Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} height"); } else if (rectangle.Contains(x - 1, y)) { ins.Add(rectangle); rectangle.Width = rectangle.Width + 1; // Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} width"); } } } if (ins.Count == 0) { var cRectangle = new CRectangle(x, y, gapHeight * 5, gapHeight); rt.Add(cRectangle); rtc.Add(cRectangle); // Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} new"); } if (ins.Count > 1) { var ba = ins[0]; for (int index = ins.Count - 1; index >= 0; index--) { ba = CRectangle.Union(ba, ins[index]); rt.Remove(ins[index]); rtc.Remove(ins[index]); } rt.Add(ba); rtc.Add(ba); // Console.WriteLine($"rt: {rt.Count} rtc:{rtc.Count} union"); } } } } Console.WriteLine($"{rt.Count} boxes"); bmp.Dispose(); byte[] bytes = new byte[10000000]; bytes[0] = 0xff; RectCollection col = new RectCollection(); for (int index = 0; index < rt.Count; index++) { var cRectangle = rt[index]; var dc = CopyImage(bitmap, cRectangle); col.Rectangles.Add(new RectObject() { X = cRectangle.X, Y = cRectangle.Y, Width = cRectangle.Width, Height = cRectangle.Height, Index = index, Bitmap = dc }); } return col; /* Bitmap done = new Bitmap(bitmap.Width, bitmap.Height); var random = new Random(); using (Graphics g = Graphics.FromImage(done)) { foreach (var rectangle in rt) { g.FillRectangle(new SolidBrush(Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))), rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); } } return done; */ }
public static CRectangle Union(CRectangle a, CRectangle b) { int x1 = Math.Min(a.X, b.X); int x2 = Math.Max(a.X + a.Width, b.X + b.Width); int y1 = Math.Min(a.Y, b.Y); int y2 = Math.Max(a.Y + a.Height, b.Y + b.Height); return new CRectangle(x1, y1, x2 - x1, y2 - y1); }
public static Bitmap CopyImage(Bitmap srcBitmap, CRectangle section) { // Create the new bitmap and associated graphics object Bitmap bmp = new Bitmap(section.Width, section.Height); Graphics g = Graphics.FromImage(bmp); g.DrawImage(srcBitmap, new Rectangle(0, 0, section.Width, section.Height), new Rectangle(section.X, section.Y, section.Width, section.Height), GraphicsUnit.Pixel); // Clean up g.Dispose(); // Return the bitmap return bmp; }