Search code examples
c#.netimagesharp

How to create Gif .net Core2 , with ImageSharp


Is the any way to create a gif from some jpegs in .net core2 with ImageSharp?

I could create a gif from some jpegs with Magick.Net, but it does'n t work on Linux.

I wanna do this on Ubuntu 14.

EDIT

I could create a Gif from Jpegs with ImageSharp. This is my source code:

        var ite = fsArray.GetEnumerator(); // fsArray is FileStream Array
        ite.MoveNext();
        using (var image1 = Image.Load(ite.Current.Name))
        {
            image1.Mutate(x => x.Resize(width, height));

            // loop
            while (ite.MoveNext())
            {
                using (var image2 = Image.Load(ite.Current.Name))
                {
                    image2.Mutate(x => x.Resize(width, height));
                    image2.Frames.First().MetaData.FrameDelay = interval;
                    image1.Frames.AddFrame(image2.Frames.First());
                }
            }
            msGif = new FileStream("result.gif", FileMode.CreateNew);
            var gifEnc = new SixLabors.ImageSharp.Formats.Gif.GifEncoder();
            image1.Save(msGif, gifEnc);
            msGif.Close();
        }

Solution

    1. Load your two images
    2. Add the first frame from the second image (after scaling) as an ImageFrame<T> to the Frames property on the first image.
    3. Save the output image as a gif.

    Quantization automatically happens when saving the image as a gif. Currently a separate palette will be generated for each frame.

    using (var image1 = Image.Load(instream1))
    using (var image2 = Image.Load(instream2))
    {
      image2.Mutate(x => x.Resize(image1.Width, image1.Height));
      image1.Frames.AddFrame(image2.Frames[0]);
    
      image1.Save(outstream, new GifEncoder());
    }