Display GIF animation on WinForm

Source: Internet
Author: User
Display GIF animation on WinForm

The ProgressBar on WinForm, to be honest, is really not flattering, too rigid, too ugly, even if it is made to display the processing progress in real time, it still cannot escape "ugly ". A small circle that is being processed can be seen everywhere in the current web to indicate that it is being processed. Although it is not real-time, it is just a GIF animation, however, it always feels more appealing than the progressBar on winform. Can I use this GIF animation to display the processing progress on winform? The answer is obviously yes. I heard that pictureBox can display GIF animation. You just need to set the image attribute of the control to the GIF animation we refer to, compile, run, and oh, when GIF animation strikes, it does not get up at all, but stays at a certain frame. IT seems that this shortcut is not feasible in the IT industry.

Then we should use the ImageAnimator. Animate () method. If we find that the language cannot be organized, we can still directly look at the code to get faster and feel sad .... I am not clear about all the things...

Code

 // First define the private variable

Private Image m_img = null;

Private EventHandler evtHandler = null;

// Reload the OnPaint method of the current winform. When the interface is re-painted, a frame is displayed in the current gif.

Protected override void OnPaint (PaintEventArgs e)

{

Base. OnPaint (e );

If (m_Image! = Null)

{

// Obtain the frame to be rendered next to the current GIF animation.

UpdateImage ();

// Display the frames to be rendered in the current GIF animation at a certain position on the interface.

E. Graphics. DrawImage (m_Image, new Rectangle (145,140, m_Image.Width, m_Image.Height ));

}

}

// Implement the Load Method

Private Load (object sender, EventArgs e)

{

// Associate a delegate with a processing method

EvtHandler = new EventHandler (OnImageAnimate );

// Obtain the GIF animation file to be loaded

M_img = Image. FromFile (Application. StartupPath + "\ loading.gif ");

// Call the start animation Method

BeginAnimate ();

}

// Start the animation

Private void BeginAnimate ()

{

If (m_img! = Null)

{

// When a GIF animation changes a frame every certain time, an event is triggered. This method is used to change each frame of the current image, the method associated with the current delegate is called.

ImageAnimator. Animate (m_img, evtHandler );

}

}

// Delegate associated Method

Private void OnImageAnimate (Object sender, EventArgs e)

{

// In this method, only the current winfor repainting is performed, and then the OnPaint () method of the winform is called for repainting)

This. Invalidate ();

}

// Obtain the frames to be rendered next to the current GIF animation. This frame is operated upon any next operation on the current GIF animation)

Private void UpdateImage ()

{

ImageAnimator. UpdateFrames (m_Image );

}

// Close the display animation. This method can be called when winform is disabled or when a button is triggered to stop rendering the current GIF animation.

Private void StopAnimate ()

{

M_Image = null;

ImageAnimator. StopAnimate (m_Image, evtHandler );

}

Finally, you can see the GIF animation. If the left or top of the animation is marked with a text "loading .... ", isn't this the same effect as web2 and 0, but the implementation is much more complicated...

Transferred from:

Http://www.cnblogs.com/RascallySnake/archive/2010/02/07/1665596.html

The ProgressBar on WinForm, to be honest, is really not flattering, too rigid, too ugly, even if it is made to display the processing progress in real time, it still cannot escape "ugly ". A small circle that is being processed can be seen everywhere in the current web to indicate that it is being processed. Although it is not real-time, it is just a GIF animation, however, it always feels more appealing than the progressBar on winform. Can I use this GIF animation to display the processing progress on winform? The answer is obviously yes. I heard that pictureBox can display GIF animation. You just need to set the image attribute of the control to the GIF animation we refer to, compile, run, and oh, when GIF animation strikes, it does not get up at all, but stays at a certain frame. IT seems that this shortcut is not feasible in the IT industry.

Then we should use the ImageAnimator. Animate () method. If we find that the language cannot be organized, we can still directly look at the code to get faster and feel sad .... I am not clear about all the things...

Code

 // First define the private variable

Private Image m_img = null;

Private EventHandler evtHandler = null;

// Reload the OnPaint method of the current winform. When the interface is re-painted, a frame is displayed in the current gif.

Protected override void OnPaint (PaintEventArgs e)

{

Base. OnPaint (e );

If (m_Image! = Null)

{

// Obtain the frame to be rendered next to the current GIF animation.

UpdateImage ();

// Display the frames to be rendered in the current GIF animation at a certain position on the interface.

E. Graphics. DrawImage (m_Image, new Rectangle (145,140, m_Image.Width, m_Image.Height ));

}

}

// Implement the Load Method

Private Load (object sender, EventArgs e)

{

// Associate a delegate with a processing method

EvtHandler = new EventHandler (OnImageAnimate );

// Obtain the GIF animation file to be loaded

M_img = Image. FromFile (Application. StartupPath + "\ loading.gif ");

// Call the start animation Method

BeginAnimate ();

}

// Start the animation

Private void BeginAnimate ()

{

If (m_img! = Null)

{

// When a GIF animation changes a frame every certain time, an event is triggered. This method is used to change each frame of the current image, the method associated with the current delegate is called.

ImageAnimator. Animate (m_img, evtHandler );

}

}

// Delegate associated Method

Private void OnImageAnimate (Object sender, EventArgs e)

{

// In this method, only the current winfor repainting is performed, and then the OnPaint () method of the winform is called for repainting)

This. Invalidate ();

}

// Obtain the frames to be rendered next to the current GIF animation. This frame is operated upon any next operation on the current GIF animation)

Private void UpdateImage ()

{

ImageAnimator. UpdateFrames (m_Image );

}

// Close the display animation. This method can be called when winform is disabled or when a button is triggered to stop rendering the current GIF animation.

Private void StopAnimate ()

{

M_Image = null;

ImageAnimator. StopAnimate (m_Image, evtHandler );

}

Finally, you can see the GIF animation. If the left or top of the animation is marked with a text "loading .... ", isn't this the same effect as web2 and 0, but the implementation is much more complicated...

Transferred from:

Http://www.cnblogs.com/RascallySnake/archive/2010/02/07/1665596.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.