Reprint please indicate the source http://blog.csdn.net/sinat_23079759/article/details/77989301
C # itself is not the tool class that makes GIF, so this class can only write on its own. However, both the PictureBox control and image support an assignment gif, that is, you can assign the GIF read from a file directly to the image and then display it to the control.
Such as
String Gif_temp_path = "d://temp.gif";//gif file
Image gif_img = Image.FromFile (Gif_temp_path);
pictureBox1.Image = gif_img;
You can display the GIF under the D://temp.gif path to the PictureBox1 control.
So how do we make our own gif?
Give me a method of my own:
As long as you pass multiple pictures (image [] img_list) as an array, you can generate a GIF of these pictures and save them to the specified directory.
private void Make_gif (int delay_time, Image [] img_list, BOOL repeat, BOOL Fanzhuan)//delay_time two frame interval; repeat repeat; Fanz Huan whether to reverse play
{
int img_num = img_list. length;//need to make the number of pictures
animatedgifencoder mygif = new Animatedgifencoder ();
String Gif_temp_path = "D://temp.gif"//generated GIF file, path to save
mygif. Start (Gif_temp_path);
Mygif. Setdelay (delay_time); Interval//-1 per two frames
: No repetition, 0: Repeat
//repeat 0:1
mygif. Setrepeat (Repeat 0:-1);
for (int i = 0; i < img_num i++)
{
mygif. Addframe (Img_list[i]);
if (Fanzhuan)//Add GIF reverse playback function
{for
(int i = (img_num-2) >0? (img_num-2)): 0; i > 0; i--)
{
mygif. Addframe (Img_list[i]);
}
Mygif. Finish ();
}
"?:" for ternary operators, A?B:C said: If A is true, then A?b:c value is B, otherwise c.
When you copy this code into your engineering code, you find that animatedgifencoder this class error, see "Show possible patches," and there is no using option,
That means this class is not the C # language itself.
Yes, this class is written by ourselves (not by me, of course).
This resource I have put on the CSDN, click here to download.
A total of four classes are required, Animatedgifencoder,gifdecoder,neuquant,lzwencoder
As long as you create a new C # class file under your project, put these four classes in, you can call the. Of course, if not too messy, direct and form class together also line.
A little bit of trouble is that if the gif you want to generate is directly displayed in PictureBox, you must generate a GIF to an address and read from this address.
Remember PictureBox every time you show a new GIF picture, you need to Dispose a bit, otherwise it will not update oh.
The specific code is: if (picturebox1.image!= null) {PictureBox1.Image.Dispose ();}
Here is another piece of code, so that you can modify a picture of the pixel value, and generate a new picture. So we can make our own code to create cool GIF. (such as the effect of a gradient from one picture to another picture.) )
The processing of the image should be used to Bitmap, not image, so first know their mutual transformation.
Image-> Bitmap
Image IMG1;
...
Bitmap Img_bitmap = new Bitmap (IMG1);
Bitmap-> Image
(direct assignment is good)
Image Img2 = Img_bitmap;//img_bitmap is a picture of a known bitmap type
The next two pieces of code, the Bitmap type of image into a three-dimensional plastic array, and three-dimensional plastic array into Bitmap image.
This allows us to work with the image by modifying the data in the "three-dimensional shape array".
public static int[,,] get_pic_int (Bitmap bitimg)//Bitmap converted to int[,,] type
{
int[,,] img_int = new int[bitimg. Width, Bitimg. Height, 3];
for (int i = 0; i < bitimg. Width; i++)
{for
(int j = 0; J < bitimg. Height; J + +)
{
img_int[i, j, 0] = bitimg. GetPixel (i, J). R;
Img_int[i, j, 1] = bitimg. GetPixel (i, J). G;
Img_int[i, J, 2] = bitimg. GetPixel (i, J). B;
}
}
return img_int;
}
.../To modify three-dimensional data, such as darken a bit, you can multiply each data by 0.5 (here is the image processing part) public
static Bitmap int_to_bitimg (int[,,] from, int x, int y )//int[,,] convert to bitmap type; X,y is the width-height of the generated picture
{
bitmap bitimg = new Bitmap (x, y);
Color Temp_color;
for (int i = 0; i < x; i++)
{
for (int j = 0; J < y; j +)
{
Temp_color = Color.FromArgb (255, fro M[i, J, 0], From[i, J, 1], From[i, J, 2]);
Bitimg. SetPixel (i, J, Temp_color);
}
return bitimg;
}
So you can achieve image processing.
Finally, once again, it takes four class files to generate the GIF, and the four files are downloaded at the address: click here.