/// <summary> /// Disposes resources used by this class. /// </summary> /// <param name="disposing"> /// Indicates whether this method is being called by the class's Dispose /// method (true) or by the garbage collector (false). /// </param> protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // dispose-only, i.e. non-finalizable logic _encoder.Dispose(); } // new shared cleanup logic _disposed = true; } // Uncomment if the base type also implements IDisposable // base.Dispose( disposing ); }
public void ExportAnimationAsGif() { var error = false; if (CurrentShownSpriteSheet == null) { Messager.ShowMessage(Messager.Mode.Exception, "No spritesheet loaded! Please load a sprite sheet first by clicking the button Import Spritesheet on topbar"); return; } if (CurrentShownAnimation != null) { if (CurrentShownAnimation.Frames.Count == 0) { Messager.ShowMessage(Messager.Mode.Exception, "No frames to export. The animation is empty!"); return; } var path = ShowCompleteSaveToDialog("Choose location to save GIF", @"Animated GIF(.gif)|*.gif"); if (path == null) { return; } if (path.Length == 0) { Messager.ShowMessage(Messager.Mode.Exception, string.Format("Nothing exported. The path is invalid or empty!")); return; } try { Size frameSize = Size.Empty; Point offSetMin = Point.Empty; Point offSetMax = Point.Empty; foreach (var frame in CurrentShownAnimation.Frames) { if (frame.SpriteFrame.Width > frameSize.Width) { frameSize.Width = frame.SpriteFrame.Width; } if (frame.SpriteFrame.Height > frameSize.Height) { frameSize.Height = frame.SpriteFrame.Height; } } foreach (var frame in CurrentShownAnimation.Frames) { if (frame.OffSetX < offSetMin.X) { offSetMin.X = frame.OffSetX; } if (frame.OffSetY < offSetMin.Y) { offSetMin.Y = frame.OffSetY; } if (frame.OffSetX > offSetMax.X) { offSetMax.X = frame.OffSetX; } if (frame.OffSetY > offSetMax.Y) { offSetMax.Y = frame.OffSetY; } } frameSize.Width += Math.Abs(offSetMin.X) + offSetMax.X; frameSize.Height += Math.Abs(offSetMin.Y) + offSetMax.Y; frameSize.Width = frameSize.Width > frameSize.Height ? frameSize.Width : frameSize.Height; frameSize.Height = frameSize.Width; int repeatCount = CurrentShownAnimation.Loop ? 0 : 1; ColourTableStrategy strategy = ColourTableStrategy.UseGlobal; int quality = 10; AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder { LogicalScreenSize = frameSize, RepeatCount = repeatCount, ColourTableStrategy = strategy, SamplingFactor = quality, }; var texture = CurrentShownSpriteSheet.Texture; animatedGifEncoder.Transparent = Color.Black; foreach (var frame in CurrentShownAnimation.Frames) { Bitmap frameBitMap = texture.Surface.ToBitmap(frame.SpriteFrame.Region); GifFrame gifFrame = new GifFrame(frameBitMap); gifFrame.Delay = 100 * ((int)(frame.FrameDuration)); if (gifFrame.Delay < 1) { gifFrame.Delay = 1; } gifFrame.Position = new Point((int)((frameSize.Width / 2f - frame.SpriteFrame.Width / 2f) + frame.OffSetX), (int)((frameSize.Height / 2f - frame.SpriteFrame.Height / 2f) + frame.OffSetY)); animatedGifEncoder.AddFrame(gifFrame); } animatedGifEncoder.WriteToFile(path); animatedGifEncoder.Dispose(); } catch (Exception ex) { error = true; Messager.ShowMessage(Messager.Mode.Exception, string.Format("Error on exporting GIF: {0}", ex.Message)); Vortex.Debugging.Log.Error(ex, "Error on exporting GIF"); } finally { if (!error) { Messager.ShowMessage(Messager.Mode.Message, "GIF created successfully!"); } } } else { Messager.ShowMessage(Messager.Mode.Exception, "No animations to export!"); } }