我一直在玩NGif Animator来调整动画gif的大小,它确实可以调整大小,但我尝试过的许多动画gif中的部分都被擦除了。我浏览了那个页面上的评论,没有看到其他人提到它。
为了消除由于调整大小的原因,我简单地循环遍历框架并保存每个框架。每一帧都是一个System.Drawing.Image。透明度设置为none (Color.Empty)。
这是我目前的测试方法:
GifDecoder gifDecoder = new GifDecoder();
MemoryStream memoryStream = new MemoryStream();
new BinaryWriter((Stream)memoryStream).Write(imageToResize); // byte array
memoryStream.Position = 0L;
gifDecoder.Read((Stream)memoryStream);
memoryStream.Dispose();
string filename = Guid.NewGuid().ToString().Replace("-", String.Empty) + ".gif";
string output = path + @"\" + filename;
AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
animatedGifEncoder.Start(output);
animatedGifEncoder.SetRepeat(gifDecoder.GetLoopCount());
animatedGifEncoder.SetQuality(10); // They say 20 is max quality will get, I've tried higher. Makes it a little bit better but black areas instead of gray. 10 is their default.
animatedGifEncoder.SetTransparent(Color.Empty); // This is default either way
int frameCount = gifDecoder.GetFrameCount();
int num = 0;
Image frame;
Image image = null;
for (int index = frameCount; num < index; ++num)
{
frame = gifDecoder.GetFrame(num);
animatedGifEncoder.SetDelay(gifDecoder.GetDelay(num));
string fname = @"C:\Development\images\frame_" + num.ToString() + ".gif";
if (File.Exists(fname)) { File.Delete(fname); }
frame.Save(fname);
animatedGifEncoder.AddFrame(image);
}
animatedGifEncoder.Finish();
以下是正在发生的情况的一个示例:
背景消失了,它是灰色的。
它应该看起来像这样:
有没有人有过使用NGif的经验,知道什么会导致这种情况?第一帧总是可以的。在那之后的其他人都有问题,所以我猜有些东西没有从一帧到另一帧被重置(或重新读取)。我已经在他们的重置框架方法中添加了更多的东西,但到目前为止还没有帮助。现在看起来是这样的:
protected void ResetFrame()
{
lastDispose = dispose;
lastRect = new Rectangle(ix, iy, iw, ih);
lastImage = image;
lastBgColor = bgColor;
delay = 0;
transparency = false; // I don't want transparency
lct = null;
act = null;
transIndex = -1;
}
发布于 2013-08-01 03:27:27
实际上,他们的代码中有一个bug,一个字节数组没有被重置。查看他们页面上的评论以获得解决方案
https://stackoverflow.com/questions/7969287
复制相似问题