![]() ![]() you might miss some frames, but the overall animation occurs at the expected rate). Usually for animation this is still preferable (i.e. There is no guarantee that the form will actually be redrawn as frequently as you update the image. Note that the above focuses on trying to animate the image on a fixed interval. Using async and await here allows you to have code that looks like a normal loop, but which allows the method to return and for the thread to continue processing in between each invalidation (i.e. _currentImage = (Bitmap)Image.FromFile(imageIndex + ".png") Īwait Task.Delay(100) // wait 100 milliseconds } private async void start_animate(object sender, EventArgs e) Private void Form1_Paint(object sender, PaintEventArgs e)Į.Graphics.DrawImageUnscaled(_currentImage, 0, 0) Į.Graphics.DrawImageUnscaled(pictureBit, 0, 0) For example: private Bitmap _currentImage ![]() At best, this will cause the rest of your program to freeze while the image is animated, and at worst (as in your scenario) you will find that due to the way that Windows handles screen updates, the on-screen image is updated only after the handler returns, preventing you from seeing the animation at all.įor simple animation such as what you're doing, the easiest thing to do would be to update the Bitmap on a timer and invalidate the form, and have the Paint event handler only draw the one time. The problem in your code is that you are looping inside the Paint event handler. ![]() When the button is clicked, it sets a Boolean to true: private void start_animate(object sender, EventArgs e) WindowG.DrawImageUnscaled(pictureBit, 0, 0) ( pictureBit is the name of the original image) private void Form1_Paint(object sender, PaintEventArgs e)īitmap images = (Bitmap)Image.FromFile(i+".png") This is the code that I have for the Form1_Paint() method: this is applied when the form loads up. However, if I click on the screen after clicking the animate button, it displays the last image ("10.png") saved in the directory. However, when I click the animate button, it doesn't do anything. When the button is clicked, it is supposed to loop through 10 images (saved in the folder as "1.png", "2.png"…"10.png") and display them one after another, creating an animation effect. I am developing a Windows Forms application which consist of a single image on the screen and a button called animate when it loads up. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |