Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello guys,

First of all iam a beginner c sharp coding.

I have a picturebox and a timer(enabled, interval = 25).

I have inserted a gif image of a bird in the picturebox.

And in the Timer event i have written,

C#
bool positionBird = true;

private void timer1_Tick(object sender, EventArgs e)
        {
            if (PictureBox1.Location.X == Screen.PrimaryScreen.Bounds.Width)
            {
                positionBird = false;
            }
            else if (PictureBox1.Location.X == 0)
            {
                positionBird = true;
            }

            if(positionBird)
            {
                PictureBox1.Left += 1;
            }
            else
            {
                PictureBox1.Left += -1;
            }
        }


But what i want to achieve is, when the picture box touches the right boundary and condition become false, i want to flip the image of bird in the picturebox. Right now the bird is doing michael jackson's Moonwalk (lol..!!).

I tried to flip the flip the bird(mirror) using the below code.

else
            {
                PictureBox pict = new PictureBox();
                pict = PictureBox1;
                pict.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
                PictureBox1.Left += -1;
            }


But it looks weird, it shows the flip image and normal image both. Can someone help me on this. As i already told iam a beginner some simple code with explanation will be very much helpful. also can someone tell me what iam doing wrong.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Feb-14 13:28pm    
Why are you mixing the PictureBox with the bitmap (or other image)? Better forget about PictireBox; it is totally irrelevant. And don't use a timer, use a separate thread...
—SA

1 solution

Ok, a couple of things.
First off, don't use the primary screen width - you can't show your PictureBox outside your Form, so use the form width instead.
Secondly, don't look for an exact match - check for "greater than or equal to" instead. Your way works fine when you increment by one, but if you decide to vary the speed, it could easily fail.

Then, to turn it round you are very, very close: all you need to do, is change your code very slightly, and move it around:

C#
private void timer1_Tick(object sender, EventArgs e)
    {
    if (pictureBox1.Location.X >= Width)
        {
        pictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
        positionBird = false;
        }
    else if (pictureBox1.Location.X + pictureBox1.Width <= 0)
        {
        pictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
        positionBird = true;
        }

    if (positionBird)
        {
        pictureBox1.Left += 10;
        }
    else
        {
        pictureBox1.Left += -10;
        }
    }
(BTW: I moved the left hand side check, so the picture swaps over when it isn't visible)
 
Share this answer
 
Comments
KRIZTE 13-Feb-14 15:07pm    
Yeah your way works fine. but iam using an animation sprite(gif) as picturebox image. when it reaches the right boundary and return back it stops animating. thats what my real problem is.
OriginalGriff 13-Feb-14 15:38pm    
Ah! You should have said...
You can't use RotateFlip: it doesn't work with GIF images (or any animated image) because it inverts the *current* state of the image, not applies a display transform.
I suspect you might get it to work by using the Form Paint event to draw it yourself, and applying a Transform and Rotate matrix which does the actual invert for you - that might work, but I haven't tried it - but that's a long way from being a "beginner" task (Matrices do my head in and it takes me ages to work out T&R stuff properly when I do have to use it, which fortunately isn't often).

There is a simple method though: Have two GIF files, one for each direction. Then just swap images when you need it to turn round...it's the lazy, "brute-force-and-ignorance" approach, yes - but it's probably the one I'd start with on the grounds it's pretty easy to do! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900