Custom Image Auto-switching image control and Image Auto-Switching Control
Make such a control and automatically switch the image to form an animation effect.
There are too few items after the code is completed, but it is still perfect.
1 public class MyPictureBox: PictureBox 2 {3 4 Timer timer = new Timer (); 5 6 private int curPicIndex = 0; 7 8 private bool run = false; 9 10 public bool Run11 {12 get {return run;} 13 set14 {15 run = value; 16 if (value) 17 timer. start (); 18 else19 timer. stop (); 20} 21} 22 23 private List <Image> imgList = new List <Image> (); 24 25 private string [] imagepaths = null; 26 27 public string [] ImagePaths28 {29 get {return imagepaths;} 30 set31 {32 imagepaths = value; 33 34 if (imagepaths = null | imagepaths. length = 0) 35 return; 36 37 imgList. clear (); 38 39 foreach (string v in imagepaths) 40 {41 try42 {43 imgList. add (Image. fromFile (v); 44} 45 catch46 {} 47} 48} 49} 50 51/private map52 53 private int autoTimeInterval = 100; 54 55 public int AutoTimeInterval56 {57 get {return autoTimeInterval;} 58 Set59 {60 autoTimeInterval = value; 61 if (value> 50 & amp; value <5000) 62 timer. interval = value; 63} 64} 65 66 public MyPictureBox () 67 {68 base. sizeMode = PictureBoxSizeMode. stretchImage; 69 70 timer = new Timer (); 71 timer. interval = autoTimeInterval; 72 timer. tick + = (s, e) => 73 {74 timer. stop (); 75 76 if (imgList! = Null & imgList. count> 0) 77 {78 int imgcount = imgList. count; 79 if (imgcount> 0) 80 {81 curPicIndex = (curPicIndex + 1) % imgcount; 82 base. image = imgList [curPicIndex]; 83} 84 85 timer. start (); 86} 87}; 88} 89}MyPictureBox
After the control is written, it can be used normally only when it is placed on the form.
Write the following in a form:
1 // loadingPic: Load Local Image 2 private void button1_Click (object sender, EventArgs e) 3 {4 OpenFileDialog dlg = new OpenFileDialog (); 5 dlg. validateNames = true; 6 dlg. multiselect = true; 7 dlg. showDialog (); 8 this. myPictureBox1.ImagePaths = dlg. fileNames; 9} 10 11 // stop: The image stops switching 12 private void button2_Click (object sender, EventArgs e) 13 {14 this. myPictureBox1.Run = false; 15} 16 17 // run: Switch the image to 18 private void button3_Click (object sender, EventArgs e) 19 {20 this. myPictureBox1.Run = true; 21} 22 23 // set the interval of image switching to 24 private void trackBar1_ValueChanged (object sender, EventArgs e) 25 {26 this. myPictureBox1. autoTimeInterval = this. trackBar1.Value * 30; 27}Events in Form
The code is very simple, but this mode is used in many places. For example, if you create an album by yourself, you can automatically switch photos. Or, when you make a simple game, you can control the behavior and walk of people, A series of actions that return from the shot to the end of the battle to the original state (in fact, they are played continuously with one frame at a time, the same as flash ); or the water flow (two or three pieces of water are enough. When the texture of the water surface changes, the water is regarded as moving), we can also say that we can imagine all simple dynamic effects or actions, can be implemented using the Image control.
This is simple.
The best project is also accumulated through simple modules.
I did not check it carefully. If you have any problems, please forgive me!
At this age, I have entered a special State: Work, girlfriend, two-handed grasp, both hands should be hard, one cannot give up. When I wanted to steal an article, I had a good time. After all, I had to find a companion. I didn't say that: time should be spent on the cutting edge. Oh, I am so confused.