如何使用ImageSharp创建Gif .net Core2 [英] How to create Gif .net Core2 , with ImageSharp

查看:413
本文介绍了如何使用ImageSharp创建Gif .net Core2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用ImageSharp在.net core2中从某些jpeg创建gif吗?

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

我可以使用Magick.Net从某些jpeg创建gif,但是在Linux上不起作用.

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

我想在Ubuntu 14上执行此操作.

I wanna do this on Ubuntu 14.

编辑

我可以使用ImageSharp从Jpegs创建一个Gif.这是我的源代码:

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();
        }

推荐答案

  1. 加载两个图像
  2. 将第二张图像(缩放后)的第一帧作为ImageFrame<T>添加到第一张图像的Frames属性.
  3. 将输出图像另存为gif.
  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.

将图像另存为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());
}

这篇关于如何使用ImageSharp创建Gif .net Core2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆