private void ToGif(string outputFile) { var bounds = new FrameBounds[frames.Count]; int i = 0; foreach (var x in frames) { bounds[i++] = new FrameBounds(x.X, x.Y, x.Width, x.Height, x); Console.WriteLine("{0} {1} {2} {3}", x.X, x.Y, x.Width, x.Height); } var minLeft = bounds.Min(x => x.left); var minTop = bounds.Min(x => x.top); var totalBounds = new FrameBounds() { left = bounds.Min(x => x.left), top = bounds.Min(x => x.top), // Top of the image right = bounds.Max(x => x.right), bottom = bounds.Max(x => x.bottom), // Bottom of the image }; Console.WriteLine("{0} {1}", minLeft, minTop); foreach (var fb in bounds) { Console.WriteLine(fb.ToString()); fb.Move(-minLeft, -minTop); Console.WriteLine(fb.ToString()); } Console.WriteLine("{0} {1}", totalBounds.left, totalBounds.top); int w = totalBounds.Width; int h = totalBounds.Height; int minx = 0, miny = 0; using (var gif = new AnimatedGifCreator(outputFile, 0, 0)) { foreach (var b in bounds) { using (var bm = new Bitmap(w + 1, h + 1)) using (var g = Graphics.FromImage(bm)) { int x = b.left; int y = b.top; Console.WriteLine("Drawing image {0} - {1}, {2} - {3}", x, x + b.Width, y, y + b.Height); g.DrawImageUnscaled( b.frame.Image.Tile, x, y ); gif.AddFrame( bm, b.frame.delay, GifQuality.Bit8 ); } } } }
private void ToGif(string outputFile) { var bounds = new FrameBounds[frames.Count]; int i = 0; foreach (var x in frames) { bounds[i++] = new FrameBounds(x.OffsetX, x.OffsetY, x.Width, x.Height, x); Console.WriteLine("{0} {1} {2} {3}", x.OffsetX, x.OffsetY, x.Width, x.Height); } var minLeft = bounds.Min(x => x.left); var minTop = bounds.Min(x => x.top); var totalBounds = new FrameBounds() { left = bounds.Min(x => x.left), top = bounds.Min(x => x.top), // Top of the image right = bounds.Max(x => x.right), bottom = bounds.Max(x => x.bottom), // Bottom of the image }; // Make sure that the image is in bounds foreach (var fb in bounds) { fb.Move(-minLeft, -minTop); } int w = totalBounds.Width; int h = totalBounds.Height; bool checkerBoard = useCheckerboard.Checked; using (var backgroundChecker = new Bitmap(w + 1, h + 1)) { const int backgroundCheckerWidth = 8; const int backgroundCheckerHeight = 8; using (var b = new Bitmap(backgroundCheckerWidth, backgroundCheckerHeight)) { using (var g = Graphics.FromImage(b)) { var c1 = new SolidBrush(Color.Gray); var c2 = new SolidBrush(Color.DarkGray); var blockWidth = backgroundCheckerWidth / 2; var blockHeight = backgroundCheckerHeight / 2; g.FillRectangle(c1, 0, 0, blockWidth, blockHeight); g.FillRectangle(c2, blockWidth, 0, blockWidth, blockHeight); g.FillRectangle(c1, blockWidth, blockHeight, blockWidth, blockHeight); g.FillRectangle(c2, 0, blockHeight, blockWidth, blockHeight); } using (var g = Graphics.FromImage(backgroundChecker)) { var checkersX = (w / backgroundCheckerWidth) + 1; var checkersY = (h / backgroundCheckerHeight) + 1; for (int y = 0; y < checkersY; y++) { for (int x = 0; x < checkersX; x++) { g.DrawImage(b, new Point(x * backgroundCheckerWidth, y * backgroundCheckerHeight)); } } } } using (var gif = new AnimatedGifCreator(outputFile, 0, 0)) { using (var bm = new Bitmap(w + 1, h + 1)) using (var g = Graphics.FromImage(bm)) { foreach (var b in bounds) { g.Clear(Color.FromArgb(0)); if (checkerBoard) { g.DrawImageUnscaled(backgroundChecker, 0, 0); } int x = b.left; int y = b.top; Console.WriteLine("Drawing image {0} - {1}, {2} - {3}", x, x + b.Width, y, y + b.Height); g.DrawImageUnscaled( b.frame.Tile, x, y ); gif.AddFrame( bm, b.frame.delay, GifQuality.Bit8 ); } } } } }