private void ExportAsAnimatedGif(string outputFilePath) { ctrlCellularAutomata.Stop(); int numFramesPreamble = 100; int numFrames = 800; var dlg = new ProgressBox(); dlg.LabelText = "Generating animated GIF..."; dlg.ProgressRange = numFramesPreamble + numFrames; dlg.StartPosition = FormStartPosition.CenterParent; Thread thread = new Thread( new ThreadStart( () => { ExportAsAnimatedGifWorker(outputFilePath, dlg, numFramesPreamble, numFrames); } ) ); thread.Start(); DialogResult dr = dlg.ShowDialog(this); ctrlCellularAutomata.Resume(); }
private void ExportAsAnimatedGifWorker(string outputFilePath, ProgressBox box, int numFramesPreamble, int numFrames) { Cells cells = _genAlg.CaSettings.GetInitialCells(_genAlg.GaSettings.InitialStateDistribution); CellularAutomataRules rules = ctrlCellularAutomata.Rules; NeighborhoodFunction function = _genAlg.CaSettings.NeighborhoodFunction; CellPainter painter = _genAlg.CaSettings.CellStructure.Painter; for (int i = 0; i < numFramesPreamble; i++) { cells = cells.ApplyRules(rules, function); box.Invoke( new Action( () => box.Increment() ) ); } using (FileStream fs = new FileStream(outputFilePath, FileMode.Create)) using (GifEncoder encoder = new GifEncoder(fs, cells.Columns * 2, cells.Rows * 2)) { Bitmap bmp = new Bitmap(cells.Columns * 2, cells.Rows * 2); Point offset = new Point(0, 0); for (int i = 0; i < numFrames; i++) { painter.PaintBitmap(bmp, cells, offset, ctrlCellularAutomata.Colors); encoder.AddFrame(Image.FromHbitmap(bmp.GetHbitmap()), 0, 0, new TimeSpan(0)); cells = cells.ApplyRules(rules, function); box.Increment(); } } box.Finish(); }
private void ExportAsAnimatedGifWorker(string outputFilePath, ProgressBox box, int numFramesPreamble, int numFrames) { Cells cells = _genAlg.CaSettings.GetInitialCells(_genAlg.GaSettings.InitialStateDistribution); CellularAutomataRules rules = ctrlCellularAutomata.Rules; NeighborhoodFunction function = _genAlg.CaSettings.NeighborhoodFunction; CellPainter painter = _genAlg.CaSettings.CellStructure.Painter; for (int i = 0; i < numFramesPreamble; i++) { cells = cells.ApplyRules(rules, function); box.Invoke( new Action( ()=>box.Increment() ) ); } using (FileStream fs = new FileStream(outputFilePath, FileMode.Create)) using (GifEncoder encoder = new GifEncoder(fs, cells.Columns * 2, cells.Rows * 2)) { Bitmap bmp = new Bitmap(cells.Columns * 2, cells.Rows * 2); Point offset = new Point(0, 0); for (int i = 0; i < numFrames; i++) { painter.PaintBitmap(bmp, cells, offset, ctrlCellularAutomata.Colors); encoder.AddFrame(Image.FromHbitmap(bmp.GetHbitmap()), 0, 0, new TimeSpan(0)); cells = cells.ApplyRules(rules, function); box.Increment(); } } box.Finish(); }