First, let's take a look:
Then compare the two GIF images.
// The original figure is moving
// After processing, it goes backwards.
GIF is dynamic. Then, when I chatted with a friend yesterday, I sent a series of identical GIF images and looked at the same things. I want to writeProgramThe initial status of GIF is different. What do you mean. We know that GIF is composed of frames. The function I want to implement is that, for example, if a GIF has a total of ten frames, the program I wrote can generate 10 GIF files, they correspond to different initial states for loop. Later, I thought that if there were too many GIF frames, it would be slow and not practical. So I decided to simplify it and only use a reversal tool. For example, a GIF was played from left to right, after this operation, you can generate an identical GIF image, but it is played backwards.
The idea is simple, that is, first break down the GIF into many frames, then merge the frames, and then reverse the frame position before merging the frames. Because I do not understand the image processing knowledge and only want to think about it, I need to find some information about these functions, modify and test them.
Split FrameCodeAs follows:
// Decoding GIF images Public list < String > Getframes (stringppath, stringpsavedpath) {image GIF =Image. fromfile (ppath); framedimension FD = Newframedimension (GIF. framedimensionslist [ 0 ]); // Get the number of frames (GIF images may contain multiple frames, and images of other formats are generally only one frame) Intcount = GIF. getframecount (FD); List < String > Giflist = newlist < String > (); // Save frames in JPEG format For (INTI =0 ; I <count; I ++ ) {GIF. selectactiveframe (FD, I); GIF. Save (psavedpath + " \ Frame _ " + I + " . PNG " , Imageformat. PNG); giflist. Add (psavedpath + " \ Frame _ " + I + " . PNG " );} Returngiflist ;}
As you can see, a list containing all generated frame addresses is returned. Then, giflist is used as the parameter for merging.
// Obtain the decoded PNG Image \ r \ n in the temporary directory of the system. Stringtemppath = system. environment. getenvironmentvariable ( " Temp " ); List < String > Giflist = Getframes (tboxfile. Text, temppath); giflist. Reverse (); stringoutputfilepath = " New.gif " ; Animatedgifencoder AE = Newanimatedgifencoder (); AE. Start (outputfilepath); AE. setdelay ( 100 ); // Delay interval \ r \ n AE. setrepeat ( 0 );// -1: no loop, 0: Always loop playback \ r \ n For (INTI = 0 , Count = giflist. Count; I <count; I ++ ) {AE. addframe (image. fromfile (giflist [I]);} AE. Finish (); MessageBox. Show ( " Successful! The new file is saved in the same directory. " );
The class "animatedgifencoder" is used here. This is the class in the GIF. components. dll dynamic Connection Library (this library is open-source and the address is provided at the end of this article). It is found on the codeproject. First, I reverse the giflist and merge and save it to the same directory. Frames generated in the middle are saved to the temp directory for convenience.
Originally, this library split the GIF function. However, after my actual test, I found that the effect was very poor, and the black lines in the images were rampant and I couldn't see them at all. So I still use the above Code, and the relevant code is still saved in the project. If you are interested, you can test it on your own.
Project source code: GIF reversal Tool
Refer:
C # Image Processing: Obtain frames in animated GIF images
Ngif, animated GIF encoder for. net