简体   繁体   中英

Convert a List of Images to a GIf

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. 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 ); ConvertedGif = new GifConversion(); ConvertedGif.ConvertToGif( DestinationPath , myImage , myFrames );

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM