Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created July 27, 2023 09:17
Show Gist options
  • Save conholdate-gists/4e2a51d12005c5aa58a0bba86943d2d6 to your computer and use it in GitHub Desktop.
Save conholdate-gists/4e2a51d12005c5aa58a0bba86943d2d6 to your computer and use it in GitHub Desktop.
Online Make Photo Animation Free | Image to Animation APNG
const int AnimationDuration = 1000; // 1 s
const int FrameDuration = 70; // 70 ms
using (RasterImage sourceImage = (RasterImage)Aspose.Imaging.Image.Load(dataDir + "not_animated.png"))
{
ApngOptions createOptions = new ApngOptions
{
Source = new FileCreateSource("raster_animation.png", false),
DefaultFrameTime = (uint)FrameDuration,
ColorType = PngColorType.TruecolorWithAlpha,
};
using (ApngImage apngImage = (ApngImage)Aspose.Imaging.Image.Create(
createOptions,
sourceImage.Width,
sourceImage.Height))
{
int numOfFrames = AnimationDuration / FrameDuration;
int numOfFrames2 = numOfFrames / 2;
apngImage.RemoveAllFrames();
// add first frame
apngImage.AddFrame(sourceImage, FrameDuration);
// add intermediate frames
for (int frameIndex = 1; frameIndex < numOfFrames - 1; ++frameIndex)
{
apngImage.AddFrame(sourceImage, FrameDuration);
ApngFrame lastFrame = (ApngFrame)apngImage.Pages[apngImage.PageCount - 1];
float gamma = frameIndex >= numOfFrames2 ? numOfFrames - frameIndex - 1 : frameIndex;
lastFrame.AdjustGamma(gamma);
}
// add last frame
apngImage.AddFrame(sourceImage, FrameDuration);
apngImage.Save();
}
}
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dataDir + "Animated PNG.png"))
{
// Checking the type of loaded image
System.Diagnostics.Debug.Assert(image is ApngImage);
// Save to the same format
image.Save(dataDir + "same_format.png");
// Export to the other animated format
image.Save(dataDir + "animated.gif", new GifOptions());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment