ImageAnimator::Animate Method (Image^, EventHandler^)
.NET Framework (current version)
Displays a multiple-frame image as an animation.
Assembly: System.Drawing (in System.Drawing.dll)
Parameters
- image
-
Type:
System.Drawing::Image^
The Image object to animate.
- onFrameChangedHandler
-
Type:
System::EventHandler^
An EventHandler object that specifies the method that is called when the animation frame changes.
This Windows Forms application demonstrates how to draw an animated image to the screen. The image is created from the animated GIF file SampleAnimation.gif located in the same folder as the application.
#using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; public ref class AnimateImageForm : public Form { // Create a Bitmap Object. private: Bitmap^ animatedImage; private: bool currentlyAnimating; public: AnimateImageForm() { try { animatedImage = gcnew Bitmap("SampleAnimation.gif"); } catch (ArgumentException^) { MessageBox::Show("Could not read the image file " + "\"SampleAnimation.gif\",\n" + "or it is not a valid image file."); } } // This method begins the animation. public: void AnimateImage() { // Begin the animation only once. // Make sure to animate only if animatedImage was // successfully initialised. if (!currentlyAnimating && animatedImage != nullptr) { ImageAnimator::Animate(animatedImage, gcnew EventHandler(this, &AnimateImageForm::OnFrameChanged)); currentlyAnimating = true; } } private: void OnFrameChanged(Object^ , EventArgs^ ) { // Force a call to the Paint event handler. this->Invalidate(); } protected: virtual void OnPaint(PaintEventArgs^ e) override { // Begin the animation. AnimateImage(); // Get the next frame ready for rendering. ImageAnimator::UpdateFrames(); if (animatedImage != nullptr) { // Draw the next frame in the animation. e->Graphics->DrawImage(this->animatedImage, Point(0, 0)); } } }; [STAThread] int main() { Application::Run(gcnew AnimateImageForm()); }
.NET Framework
Available since 1.1
Available since 1.1
Show: