For the past 12 hours or so I have been trying to get image conversion to work with GIF images. For some reason I can't quite figure out how to convert a image strip to a GIF image. I've got GIF to image strip working, but not vice versa. I've got the methodology down I believe, but I haven't made any progress for the past 12 hours.
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(and 137) is this:
ConvertedGif = new GifConversion();
ConvertedGif.ConvertToGif( DestinationPath , myImage , myFrames );
"DestinationPath" is the save destination for the new image.
"myImage" is the image to retrieve the frames of the GIF from.
"myFrames" is the number of frames to give the GIF image.