Convert a List of Images to a GIf

假装没事ソ 提交于 2019-12-08 12:00:16

问题


I am currently trying to construct a GIF image from a source image strip, and I am catching the following exception error: System.Runtime.InteropServices.ExternalException (0x80004005): A genereric error occurred in GDI+. At ... line 55. At ... line 138. The code I am using takes 2 user inputted file paths from the console to allow the user to select which image is used as the source, and to define a path for the new file. The problem is that I keep getting exception errors that I can't make any sense out of:

public void ConvertToGif( string DestinationPath , Image myImage , int myFrames ) {
    Bitmap myBitmap = new Bitmap( myImage.Width / myFrames , myImage.Height );
    myBitmap.Save( DestinationPath , ImageFormat.Gif );

    Image myGIF = Image.FromFile( @DestinationPath );
    FrameDimension myDimensions = new FrameDimension( myGIF.FrameDimensionsList[ 0 ] );

    for( int i = 0; i < myFrames; i ++ ) {
        var DestRegion = new Rectangle( 0 , 0 , myGIF.Width , myGIF.Height );
        var SrceRegion = new Rectangle( myGIF.Width * i , 0 , myGIF.Width , myGIF.Height );
        Graphics GrDrw = Graphics.FromImage( myBitmap );
        GrDrw.DrawImage( myImage , DestRegion , SrceRegion , GraphicsUnit.Pixel );

        EncoderParameter encCompressionrParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
        EncoderParameter encQualityParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0L);
        EncoderParameters myEncoderParameters = new EncoderParameters(2);
        myEncoderParameters.Param[0] = encCompressionrParameter;
        myEncoderParameters.Param[1] = encQualityParameter;
        myGIF.SaveAdd( myBitmap , myEncoderParameters ); /*Line 55*/
    }
}

The methodology here is to create an empty bitmap, transfer the specific rectangle frame from the image strip to the bitmap, then add the bitmap as a frame to the GIF image.

Line 138 is this: ConvertedGif = new GifConversion(); ConvertedGif.ConvertToGif( DestinationPath , myImage , myFrames );


回答1:


Unfortunately GDI+ implementation of the encoders for multi-framed GIFs are not suppported you can only create multi-frame TIFFs files using GDI+.

See this article: http://www.universalthread.com/ViewPageArticle.aspx?ID=841

Small snippet from there

Unfortunately, it is not possible to create Multi-frame or Animated GIFS using GdiPlus.dll version 1 image encoders. The SaveAdd method which can be used to add frames to a multi-frame TIFF image does not work with the GIF encoder.

You can read information from GIF files but you are not permitted to create new frames etc.

There is additional metadata that you can get from the .Net GDI+ classes like the frame delay etc.

There is other articles including in www.asp.net site citing that GDI+ does not support writing GIF's

You might be better off using NGif which takes care of all of that for you.

This question has already been asked here at stackoverflow have a look at this one.

C# - Create an animated GIF image from selected images in ASP.net

Also here is NGif - http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET



来源:https://stackoverflow.com/questions/23007046/convert-a-list-of-images-to-a-gif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!