更新:
我进行了更多的研究,认为这些 ffmpeg 和 mplayer 的建议值得一试:
从一组jpeg图像创建动画gif
更新2:
来自 Rick van den Bosch 的代码也非常好,因为它让你可以访问延迟时间:
.Net(至少1.1版本,他们可能会在2.0中合并)无法通过GDI+创建动画GIF。
StringCollection stringCollection;
MemoryStream memoryStream;
BinaryWriter binaryWriter;
Image image;
Byte[] buf1;
Byte[] buf2;
Byte[] buf3;
stringCollection = a_StringCollection_containing_images;
Response.ContentType = "Image/gif";
memoryStream = new MemoryStream();
buf2 = new Byte[19];
buf3 = new Byte[8];
buf2[0] = 33;
buf2[1] = 255;
buf2[2] = 11;
buf2[3] = 78;
buf2[4] = 69;
buf2[5] = 84;
buf2[6] = 83;
buf2[7] = 67;
buf2[8] = 65;
buf2[9] = 80;
buf2[10] = 69;
buf2[11] = 50;
buf2[12] = 46;
buf2[13] = 48;
buf2[14] = 3;
buf2[15] = 1;
buf2[16] = 0;
buf2[17] = 0;
buf2[18] = 0;
buf3[0] = 33;
buf3[1] = 249;
buf3[2] = 4;
buf3[3] = 9;
buf3[4] = 10;
buf3[5] = 3;
buf3[6] = 255;
buf3[7] = 0;
binaryWriter = new BinaryWriter(Response.OutputStream);
for (int picCount = 0; picCount < stringCollection.Count; picCount++)
binaryWriter.Write(buf3, 0, 8);
binaryWriter.Write(buf1, 789, buf1.Length - 790);
if (picCount == stringCollection.Count - 1)
memoryStream.SetLength(0);
}
binaryWriter.Close();
Response.End();
正如Hans所提到的,它不受支持,因此这个第三种解决方案是RenniePet建议从两个Gif中提取帧,然后将所有帧合并在一起。
添加对System.Drawing.DLL的引用,并使用以下代码获取帧:
using System.Drawing;
using System.Drawing.Imaging;
public class GifImage
{
private Image gifImage;
private FrameDimension dimension;
private int frameCount;
private int currentFrame = -1;
private bool reverse;
private int step = 1;
public GifImage(string path)
{
gifImage = Image.FromFile(path);
dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
frameCount = gifImage.GetFrameCount(dimension);
}
public int GetFrameCount()
{
return frameCount;
}
public bool ReverseAtEnd
{
get { return reverse; }
set { reverse = value; }
}
public Image GetNextFrame()
{
currentFrame += step;
if (currentFrame >= frameCount || currentFrame < 1)
{
if (reverse)
{
step *= -1;
currentFrame += step;
}
else
{
currentFrame = 0;
}
}
return GetFrame(currentFrame);
}
public Image GetFrame(int index)
{
gifImage.SelectActiveFrame(dimension, index);
return (Image)gifImage.Clone();
}
}
我们可以像这样提取所有的帧:
private static readonly string tempFolder = @"C:\temp\";
static void Main(string[] args)
{
CombineGifs(@"c:\temp\a.gif", @"c:\temp\b.gif");
}
public static void CombineGifs(string firstImageFilePath, string secondImageFilePath)
{
int frameCounter = ExtractGifFramesAndGetCount(firstImageFilePath, 0);
int secondframeCounter = ExtractGifFramesAndGetCount(secondImageFilePath, frameCounter);
string filePathOfCombinedGif = CombineFramesIntoGif(0, secondframeCounter);
}
private static int ExtractGifFramesAndGetCount(string filePath, int imageNameStartNumber)
{
GifImage gifImage = new GifImage(filePath);
gifImage.ReverseAtEnd = false;
int frameCounter = imageNameStartNumber + gifImage.GetFrameCount();
for (int i = imageNameStartNumber; i < frameCounter; i++)
{
Image img = gifImage.GetNextFrame();
img.Save(tempFolder + i.ToString() + ".png");
}
return frameCounter;
}
下一步,我们使用
NGif将所有帧组合成单个动画gif。
下载代码,打开解决方案并编译组件项目以获取DLL
Gif.Components.dll
,然后在您的解决方案中引用该DLL。
private static string CombineFramesIntoGif(int startFrameCount, int endFrameCount)
{
List<string> imageFilePaths = new List<string>();
for (int i = startFrameCount; i < endFrameCount; i++)
{
imageFilePaths.Add(tempFolder + i.ToString() + ".png");
}
string outputFilePath = tempFolder + "test.gif";
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.Start(outputFilePath);
e.SetDelay(500);
e.SetRepeat(0);
for (int i = 0; i < imageFilePaths.Count; i++)
{
e.AddFrame(Image.FromFile(imageFilePaths[i]));
}
e.Finish();
return outputFilePath;
}