سؤال

How can I show an animated image in my Form, controlling its size and time duration?

I tryed something like this:

private void AnimImage()
{
    PicBox.Enabled = true;
    PicBox.Visible = true;                
    System.Threading.Thread.Sleep(5000);
    PicBox.Visible = false;
    PicBox.Enabled = false;
}
هل كانت مفيدة؟

المحلول

To display an animated image on your Form,do the following;

1.)Drop a PictureBox on your Form.

2.)In the Properties Window of designer,change the image property so it contains the path to your image.

3.)Resize it as per your needs.

That's it,now try to run the project,if no exception is thrown you will see your image animating in the PictureBox.
Anytime during the course of execution of the project if you want to change the image,use the statement below;

pictureBox1.Load("Path to a new image");//Assuming you haven't renamed the PictureBox.

Additionally,if you would like to do the stuff by hand,read along;

private void DisplayImage()
{
    PictureBox pictureBox1=new PictureBox();
    pictureBox1.Location=new Point(Use appropriate values to place the control);
    this.Controls.Add(pictureBox1);
    pictureBox1.Load("Path to a image to display");
}

When you don't want the PictureBox to be shown,set its visible property to false just like other users said,in this way;

pictureBox1.Visible=false;

and to get it back use the code below;

pictureBox1.Visible=true;

Update :

To display the image only for 5 seconds do this;

Drop a Timer on your Form.

Set its Interval property to 5000 Milliseconds.

Create a new Event for its Tick Event (locate Tick event in Events Window and double click it).

Next modify DisplayImage() so it looks like :

private void DisplayImage()
{
    timer1.Start();
    PictureBox pictureBox1=new PictureBox();
    pictureBox1.Location=new Point(Use appropriate values to place the control);
    this.Controls.Add(pictureBox1);
    pictureBox1.Load("Path to a image to display");
}

Next define an integer field(outside all functions) named count,like this;

private int count=0;

Now modify timer1_Tick() event so it looks like below;

private void timer1_Tick(object sender, EventArgs e)
{
    count++;
    if (count == 5)
    {
        SourcePictureBox.Image = null;
        count = 0;
    }
}

That should do the job.Anything else,please let me know.

نصائح أخرى

Add a picturebox to your form and Add the .gif image in the picturebox.Make the picture box visibility to true while loading the form.

Please make sure that the picturebox is enabled at the time of loading otherwise there wont be any animation of the image in the windows form.

open your form in your solution explorer open your project name, open properties and right click the resources to add to them. drag your .Gif image and set it to enabled and save.

add a pictureBox and set the image as your Gif (NOT BACK GROUND IMAGE OR IT WILL NOT WORK) DO NOT DOCK OR IT RESETS THE TIMER SO GIF WILL LOOP.

add a timer, set it to 5000 (5 seconds) and Enable it.

double click your picture box and enter

pictureBox1.Visible = false;  (assuming you have not renamed your picturebox) (NOTE this is a back up encase the timer fails)

go back to your form and double click your timer, and add pictureBox1.Visible = false; timer1.Stop(); (prevent the timer starting on load up)

your image should now close once time is up (just click it if not)

if you need your Gif to start on a button click in private void that has initialize written in it add pictureBox1.Visible = false;

now add your button and code the following to button1_click

pictureBox1.Visible = true; Timer1.Start(); (this is the code used to start the timer upon button click) enter code here

IF THIS CAUSES A LOOPING ERROR i.e. gif does not hide, simply put timer.Stop(); in the picturebox_click.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top