开发者

How to resize animated gif file using imagemagick without destroying animation using C#?

开发者 https://www.devze.com 2023-02-09 00:50 出处:网络
I am using imagemagick DLL (Refer: http://www.imagemagick.org)for the resize image, But when I re-sized animated GIF image then it 开发者_如何学JAVAgoing screw.

I am using imagemagick DLL (Refer: http://www.imagemagick.org) for the resize image,

But when I re-sized animated GIF image then it 开发者_如何学JAVAgoing screw.

I using below code for re-size image ( image type are png, gif, jpg, bmp, tif ...)

ImageMagickObject.MagickImage imgLarge = new ImageMagickObject.MagickImage();
 object[] o = new object[] { strOrig, "-resize", size, "-gravity", "center", "-colorspace", "RGB", "-extent", "1024x768", strDestNw };
imgLarge.Convert(ref o);

How can I fixed it. see the result image

How to resize animated gif file using imagemagick without destroying animation using C#?


I think you have to extract every single frame from the gif first, resize every single frame and then put it back together.

Edit: like this? Not tested nor builded...

int maxFrames=32;
ImageMagickObject.MagickImage imgLarge = new ImageMagickObject.MagickImage();  

// first extract all frames from gif to single png files
for(int frame=0; frame<maxFrames;frame++)
{
   object[] o = new object[] { String.Format(strOrig+"[{0}]", frame)
       ,  String.Format("tmp{0}.png", frame) };
   imgLarge.Convert(ref o);    
}
// resize every single png files
// add resized filenames to stringbuilder
StringBuilder filenames = new StringBuilder();
for(int frame=0; frame<maxFrames;frame++)
{
   object[] o = new object[] { String.Format("tmp{0}.png", frame)
                , "-resize"
                , size 
                , "-gravity"
                , "center"
                , "-colorspace"
                , "RGB"
                , "-extent"
                , "1024x768"
                , String.Format("tmp-resized{0}.png", frame) };
   filenames.Append(String.Format("tmp-resized{0}.png", frame));
   filenames.Append(Environment.NewLine);
   imgLarge.Convert(ref o);    
}
// write resized filenames to file
File.WriteAllText("tmp-resized-files.txt", filenames);
// create resize animated gif based on filenames in the tmp-resized-files.txt
   object[] o = new object[] { "@tmp-resized-files.txt"
       ,  strDestNw };
   imgLarge.Convert(ref o);    
0

精彩评论

暂无评论...
验证码 换一张
取 消