In Visual C + +. NET simple implementation of GIF animation

Source: Internet
Author: User
Tags time interval valid

Since the introduction of GIF animation format in the past more than 10 years, but has been busy with C + + programmers. Far do not say, say these years, various methods can be described in a variety of ways, some direct reading files, a line of the tireless analysis, some use ipicture, a lot of play interface, and some packaged into COM, who do not know how he achieved ... Now with GDI +, we are more busy, so I wrote this article.

In fact, just use the. NET Framework to encapsulate a few functions, you can easily handle, here is the tool to use is vs.net2003.

First step: Create a new one. NET Form Engineering

Step Two: Add member variables and messages (that is, behavior and appearance), just double-click the blank system will automatically fill out the default function, add three functions in the diagram is enough.

Step three: Add the member variable image and initialize in Form1_Load

Program code:
Private
Image *image;
Private:system::void form1_load (System::Object * sender, System::EventArgs * e)
{
Read files by Path
Image=image::fromfile (L) test picture. GIF ");
}
Private:system::void form1_closed (System::Object * sender, System::EventArgs * e)
{
if (image) Image->dispose ();
}
Fourth step: OK, the animation file has been read, now the task is to show it
Private:system::void form1_paint (System::Object * sender,
System::windows::forms::P Ainteventargs * e)
{
Animation is enabled in Form1_paint, the function of which is to read the time information of each object in the picture, and then every time
Call once Onpaintgif
Imageanimator::animate (Image, New EventHandler (this,onpaintgif));
Show pictures
E->graphics->drawimage (0,0,image->width,image->height);
Flip a picture back one page at a time interval
Imageanimator::updateframes (image);
}
Private:system::void onpaintgif (object* sender, eventargs* e)
{
Time to show the next picture, the display area is not valid
This->invalidate ();
}

In this way, the animated GIF format can be correctly displayed, simple, just a few lines to solve.

........ However, hehe, you will find that the picture flashes a flash, it is not good to see ... What about it? With double buffering method? It's not the same! The problem with picture Flash Mingshuo can be seen on the forums almost every day, and I'm here to explain why. Large data when the flashing is because the computer is too late to load data, at this time can use double buffering method; But when the data volume is not big, the picture is flashing because when the program screen is invalid, the frame will use the background color to erase, this will flash, then use double buffering method is invalid. The latter in MFC we can respond to ONERASEBKGND () to solve, but in. NET not found this message, how to do? In fact, just avoid the screen is not valid, please look down:

Program code:
Private
Image *image;
Graphics *p;//Add a variable to display
Private:system::void form1_load (System::Object * sender, System::EventArgs * e)
{
Image=image::fromfile (L) test picture. GIF ");//Read files by path
P=graphics::fromhwnd (This->handle)//Create Graphics by window handle
Imageanimator::animate (Image, New EventHandler (this,onpaintgif));//Start animation
}
Private:system::void form1_closed (System::Object * sender, System::EventArgs * e)
{
if (p) p->dispose ();
if (image) Image->dispose ();
}
Private:system::void onpaintgif (object* o, eventargs* e)
{
P->drawimage (image,0,0,image->width,image->height)//Display picture
Imageanimator::updateframes ()//Turn the picture backward, because Onpaintgif () itself is started by time
, so Updateframes () don't care about the parameters.
}
Private:system::void form1_paint (System::Object * sender,
System::windows::forms::P Ainteventargs * e)
{
Oh, this can not be done, delete all text in the property page to automatically delete the code
}

Is it easy too? This will also solve the problem of flashing the image. The effect of some pictures is as follows, the image file is packaged in the Code folder, the program in the code bag can read a variety of pictures and can click anywhere to drag

To sum up:

    1. Because the. NET encapsulation class is used, it is easy to animate () to start the animation and then Updateframes () the page;
    2. First sweat, because the above two functions of the specific functions and methods of use (especially Updateframes () parameters) can not find a detailed description of the document, so I guess .... Please know the help of the people to leave a message below;
    3. This document should be the first to describe VC write. NET Framework program of the Chinese original document, a lot of things have no data, are the author of their own, such as by window handle to create graphics that sentence, began to think This->getsafehwnd (); later xxx.m_ an hWnd; And then later ... Finally found the This->handle, and then Khan ... In addition compiler prompts don't enter own code in InitializeComponent (), you don't lose, otherwise ... 555~~ cost is very heavy na ~ ~ Khan ^_^!
    4. I in the code file in the way to do a bit of expansion, you can read most commonly used image files, but not very perfect, as for everyone to leave a job ah. Now the loading speed of the drawing tools such as ACDSee is getting slower, and our small program to run faster than it that much faster, since our own program can show most of the common pictures, then double-click the image file, with our program quickly open browsing, not cool, fast?

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.