您的位置:首页 > 编程语言 > C#

C#制作gif

2016-12-04 17:32 323 查看

用到的库

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: