- 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
- 本站IM群,请自行选择。请各位朋友按照自己喜好加入。加入群后请及时发言,防止被清理。谢谢您的合作!!!
- QQ群:Y①WEB开发(ASP.NET)号码:7351660 QQ群:Y②WEB开发(ASP+.NET)号码:11864905
- QQ群:Y③WEB开发(DIV+CSS)号码:16610506 QQ群:Y④WEB开发(JS+AJAX)号码:16143998
- QQ群:Y⑤WEB开发(新手)号码:12777715 MSN群:yaosansi[at]126.com
一个Gif图像文件,是有几个文件进行合成的,因此处理此类文件的时候,不能像Jpeg或者Bmp文件那样处理。需要把Gif文件拆分帧的形式,然后对每一帧进行处理,处理完后再合成Gif。
其实网上有个例子对于Gif处理非常详细,地址如下。
http://www.codeproject.com/dotnet/NGif.asp
1: /* create Gif */
2: //you should replace filepath
3: String [] imageFilePaths = new String[]{"c:\\01.png","c:\\02.png","c:\\03.png"};
4: String outputFilePath = "c:\\test.gif";
5: AnimatedGifEncoder e = new AnimatedGifEncoder();
6: e.Start( outputFilePath ); 7: e.SetDelay(500); 8: //-1:no repeat,0:always repeat
9: e.SetRepeat(0); 10: for (int i = 0, count = imageFilePaths.Length; i < count; i++ )
11: { 12: e.AddFrame( Image.FromFile( imageFilePaths[i] ) ); 13: } 14: e.Finish(); 15: /* extract Gif */
16: string outputPath = "c:\\";
17: GifDecoder gifDecoder = new GifDecoder(); 18: gifDecoder.Read( "c:\\test.gif" );
19: for ( int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++ ) 20: { 21: Image frame = gifDecoder.GetFrame( i ); // frame i 22: frame.Save( outputPath + Guid.NewGuid().ToString() 23: + ".png", ImageFormat.Png ); 24: } 但是对于一个Gif进行拆分,其实Image对象本身就支持,例如对于一个Gif文件拆分成Jpeg文件方式,可以按照如下的方式进行处理。
1: using System.Drawing.Drawing2D;
2: 3: using System.Drawing.Imaging;
4: 5: Image imgGif = Image.FromFile(@"d:\test.gif");
6: 7: //Create a new FrameDimension object from this image
8: 9: FrameDimension ImgFrmDim = new FrameDimension( imgGif.FrameDimensionsList[0] );
10: 11: //Determine the number of frames in the image
12: 13: //Note that all images contain at least 1 frame,
14: 15: //but an animated GIF will contain more than 1 frame.
16: 17: int nFrameCount = imgGif.GetFrameCount( ImgFrmDim );
18: 19: // Save every frame into jpeg format
20: 21: for( int i = 0; i < nFrameCount; i++ )
22: 23: { 24: 25: imgGif.SelectActiveFrame( ImgFrmDim, i ); 26: 27: imgGif.Save( string.Format( @"d:\Frame{0}.jpg", i ), ImageFormat.Jpeg );
28: 29: }
订阅本站,阅读更多文章