/// <summary> /// Generates transition images of #left and #right pixels to left and right respectfully. /// </summary> /// <param name="number"></param> /// <param name="left"></param> /// <param name="right"></param> /// <returns></returns> public static List <FastBitmap> GenerateTransitions(FastBitmap number, int left, int right) { List <FastBitmap> transitions = new List <FastBitmap>(); for (int x = -1 * left; x <= right; x++) { if (x == 0) { continue; } number.UnlockBitmap(); Bitmap bitmap = new Bitmap(number.Width, number.Height, number.Bitmap.PixelFormat); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.FromArgb(0, 0, 0)); if (x < 0) { g.DrawImageUnscaled(number.Bitmap.Clone(new Rectangle(Math.Abs(x), 0, number.Width - Math.Abs(x), number.Height), number.Bitmap.PixelFormat), 0, 0); } else { g.DrawImageUnscaled(number.Bitmap.Clone(new Rectangle(0, 0, number.Width - x, number.Height), number.Bitmap.PixelFormat), x, 0); } number.LockBitmap(); transitions.Add(new FastBitmap(bitmap)); } return(transitions); }
public static Rectangle BoundingRectangle(Bitmap bmp, Rectangle bounds, int bound_color) { FastBitmap fbmp = new FastBitmap(bmp); int s_x = 0, e_x = 0, s_y = 0, e_y = 0; bool done = false; for (int x = bounds.X; x < bounds.X + bounds.Width && !done; x++) { for (int y = bounds.Y; y < bounds.Y + bounds.Height; y++) { if (fbmp.GetPixel(x, y) == bound_color) { s_x = x; done = true; break; } } } done = false; for (int x = bounds.X + bounds.Width - 1; x >= bounds.X && !done; x--) { for (int y = bounds.Y; y < bounds.Y + bounds.Height; y++) { if (fbmp.GetPixel(x, y) == bound_color) { e_x = x; done = true; break; } } } done = false; for (int y = bounds.Y; y < bounds.Y + bounds.Height && !done; y++) { for (int x = bounds.X; x < bounds.X + bounds.Width; x++) { if (fbmp.GetPixel(x, y) == bound_color) { s_y = y; done = true; break; } } } done = false; for (int y = bounds.Y + bounds.Height - 1; y >= bounds.Y && !done; y--) { for (int x = bounds.X; x < bounds.X + bounds.Width; x++) { if (fbmp.GetPixel(x, y) == bound_color) { e_y = y; done = true; break; } } } fbmp.UnlockBitmap(); //Console.WriteLine(s_x + " " + e_x + " " + s_y + " " + e_y); Rectangle crop_rect = new Rectangle(s_x, s_y, e_x - s_x + 1, e_y - s_y + 1); return(crop_rect); }