Because a project needs to convert a set of continuous photos into animated forms for transmission, we have studied this aspect, later, we found a project named NGif In the open-source project. Through the methods provided in this project, we can directly implement this requirement. However, I found that this project is not directly written in C #. On the contrary, it calls a Java class and converts it to C # code, this is certainly not within the scope of our research today, because this project will involve some problems of GIF file encoding and decoding. First, download this project: NGif. After the project is compiled, we will get the Gif. Components. dll file, which is the GIF library we will use today. Next, let's learn how to use this library to encode and decode GIF animations!
If we want to generate multiple photos as an animation, we only need:
// Define the animation resource string [] mFrames = new string [] {"Enter the image set to generate the animation here "}; // define the final generated file path string mOutputFile = "enter the final generated GIF animation file path here"; // obtain the encoder instance AnimatedGifEncoder mEncoder = new AnimatedGifEncoder (); // start encoding mEncoder. start (mOutputFile); // set the time of each frame to 1 second mEncoder. setDelay (1000); // set the screen to be repeated, 0 to repeated, and-1 to mEncoder. setRepeat (-1); // Add each frame image for (int I = 0; I <mFrames. count; I ++) {mEncoder. addFrame (mFrames [I]);} // end code mEncoder. finish ();
Similarly, for GIF files, we can obtain each frame of the file through decoding. The Code is as follows:
// Obtain the decoder instance GifDecoder mDecoder = new GifDecoder (); // read the GIF file mDecoder. read ("Enter the path of the GIF file here"); for (int I = 0; I <mDecoder. getFrameCount (); I ++) {// obtain the Image mFrame = mDecoder for each frame. getFrame (I); // stores the mFrame of the frame. save ("fill in the storage path for the image here ");}
From the above method, we can see that using this library to decode and encode GIF is very simple, but I found a problem when using this library. The project I want to achieve is to take screenshots repeatedly to get continuous images and then generate animation files based on the library. Theoretically, this is no problem, however, during the test, I found that the animation files generated in this way are very large, and there is a strong sense of stagnation during browsing, and there is no video feeling at all. If you have a small image, this may be possible, but for my project, it is obvious that it cannot meet the requirements, because the screenshots obtained from screenshots are relatively large, so it will take a lot of time to process them, there is such a project on the Internet that captures the screen by capturing the screen, but the efficiency is too low. Even if we still need to wait for a long time to put it in the thread, this project has the right to play.