アニメーションGIFのサイズを変更するコードがあります。それが役立つ場合、コードは常に画像のサイズを小さいサイズに変更します。(今のところ、それらを大きくする必要はありません)
実際のリサンプリングを行うために、Atalasoftのdotimageライブラリとそのサンプルコードを使用しています。このコードは、アニメーションGIFをディスクから読み取り、フレームを反復処理して、各フレームのサイズを新しいサイズに変更することになっています。これは、gifアニメーションに同じサイズのフレームが含まれているが、異なるサイズのフレームでアニメーションのサイズを変更すると、アニメーションが破損する場合(サイズ変更後にフレームが互いに正しくオーバーラップしない場合)は正常に機能します。これは、コードが新しいオフセットを計算していないためだと思います。正しく。
オフセットを正しく計算していないのは、このコード行だと思います。Point point = new Point((int)(frame.Location.X * ratio)、(int)(frame.Location.Y * ratio));
完全なサイズ変更ルーチンは次のとおりです。
static private void GenerateGifImage(FileStream fileStream, int OutputWidth, int OutputHeight)
{
// MemoryStream InputStream = new MemoryStream();
FileStream InputStream = fileStream;
// fileStream.Write(InputStream.GetBuffer(), 0, (int)InputStream.Position);
// InputStream.Seek(0, SeekOrigin.Begin);
Image InputImage = Image.FromStream(InputStream, true, false);
// this will invalidate the underlying image object in InputImage but the class properties
// will still accessible until the object is disposed
InputStream.Seek(0, SeekOrigin.Begin);
ImageInfo imageInfo = RegisteredDecoders.GetImageInfo(InputStream);
InputStream.Seek(0, SeekOrigin.Begin);
GifDecoder gifDecoder = new GifDecoder();
int count = gifDecoder.GetFrameCount(InputStream);
GifFrameCollection gifFrameCollection = new GifFrameCollection();
gifFrameCollection.Height = OutputHeight;
gifFrameCollection.Width = OutputWidth;
// gifFrameCollection.Height = gifDecoder.Frames.Height;
// gifFrameCollection.Width = gifDecoder.Frames.Width;
double ratio;
if (InputImage.Height > InputImage.Width)
{
ratio = (double)OutputHeight / (double)InputImage.Height;
}
else
{
ratio = (double)OutputWidth / (double)InputImage.Width;
}
for (int i = 0; i < count; i++)
{
GifFrame frame = gifDecoder.Frames[i];
Rectangle rectangle = new Rectangle(Point.Empty, frame.Image.Size);
int frameWidth = (int)(frame.Image.Width * ratio);
int frameHeight = (int)(frame.Image.Height * ratio);
// account for erratic rounding, seems illogical but has happened earlier when using floats instead of doubles
if (frameWidth > OutputWidth)
{
frameWidth = OutputWidth;
}
if (frameHeight > OutputHeight)
{
frameHeight = OutputHeight;
}
Size size = new Size(frameWidth, frameHeight);
// only resize if we have a measureable dimension
if (size.Width > 0 && size.Height > 0)
{
// ResampleCommand resampleCommand = new ResampleCommand(rectangle, size, ResampleMethod.NearestNeighbor);
ResampleCommand resampleCommand = new ResampleCommand(rectangle, size, ResampleMethod.NearestNeighbor);
AtalaImage atalaImage = resampleCommand.Apply(frame.Image).Image;
// save the image for debugging
// atalaImage.Save("frame" + i.ToString() + ".gif", ImageType.Gif, null);
// frame.Image.Save("frame-orig" + i.ToString() + ".gif", ImageType.Gif, null);
// AtalaImage atalaImage = frame.Image;
Point point = new Point((int)(frame.Location.X * ratio), (int)(frame.Location.Y * ratio));
// Point point = new Point((int)(frame.Location.X), (int)(frame.Location.Y));
gifFrameCollection.Add(new GifFrame(atalaImage, point, frame.DelayTime, frame.Interlaced, frame.FrameDisposal, frame.TransparentIndex, frame.UseLocalPalette));
}
}
FileStream saveStream = new FileStream("resized.gif", FileMode.Create, FileAccess.Write, FileShare.Write);
GifEncoder gifSave = new GifEncoder();
gifSave.Save(saveStream, gifFrameCollection, null);
saveStream.Close();
}