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 emg = new Image(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 bkgImg = new Image(bkgFn);

for(int i=0;i  tmp = emg.Resize(1.0 * (i + number/2) / (number + number/2), Emgu.CV.CvEnum.Inter.Nearest);
    Image con = bkgImg.Resize(emg.Width, emg.Height, Inter.Nearest); //new Image(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();

你可能感兴趣的:(C#相关)