C#制作gif

用到的库

  • emgucv
  • Gif.Components(是一个dll文件,需要自己添加到reference中)

说明

  • 实现的功能:将一幅图片由小到大旋转输出,可以设置背景图片
  • 这个功能主要是自己不想复习的时候做着玩的。。。其实没啥用(premiere几分钟就能做出相同的效果)

代码

string inputFn = "./img/1.jpg";
string outputFn = "./img/test.gif";
string bkgFn = "./img/2.jpg";
int interval = 50;
int number = 60;
Image<Bgr, Byte> emg = new Image<Bgr, byte>(inputFn);
emg = emg.Resize( 0.5, Emgu.CV.CvEnum.Inter.Nearest);

Bitmap img;
AnimatedGifEncoder gif = new AnimatedGifEncoder();
gif.SetDelay(interval);
gif.Start(outputFn);
gif.SetRepeat(0);
//gif.SetTransparent(Color.Green);
gif.SetSize(emg.Width, emg.Height);

Image<Bgr, Byte> bkgImg = new Image<Bgr, byte>(bkgFn);

for(int i=0;i<number;i++)
{
    Image<Bgr, Byte>  tmp = emg.Resize(1.0 * (i + number/2) / (number + number/2), Emgu.CV.CvEnum.Inter.Nearest);
    Image<Bgr, Byte> con = bkgImg.Resize(emg.Width, emg.Height, Inter.Nearest); //new Image<Bgr, byte>(emg.Width, emg.Height, new Bgr(0,255, 255));
    tmp = tmp.Rotate(360/number * i, new PointF(tmp.Width/2, tmp.Height/2), Inter.Cubic, new Bgr(0,0,0), true);

    for (int m = (emg.Height - tmp.Height) / 2; m < (emg.Height - tmp.Height) / 2 + tmp.Height;m++ )
    {
        for (int n = (emg.Width - tmp.Width) / 2; n < (emg.Width - tmp.Width) / 2 + tmp.Width; n++)
        {
            con.Data[m, n, 0] = tmp.Data[m - (emg.Height - tmp.Height) / 2, n - (emg.Width - tmp.Width) / 2, 0];
            con.Data[m, n, 1] = tmp.Data[m - (emg.Height - tmp.Height) / 2, n - (emg.Width - tmp.Width) / 2, 1];
            con.Data[m, n, 2] = tmp.Data[m - (emg.Height - tmp.Height) / 2, n - (emg.Width - tmp.Width) / 2, 2];
        }
    }

    img = con.ToBitmap();
    //img.MakeTransparent();
    img.Save("./img/" + i + ".bmp");
    gif.AddFrame(img);
}
gif.Finish();
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

littletomatodonkey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值