Milton Raju Paul
Milton Raju Paul

Reputation: 31

Flip Image(Mirror) in a PictureBox Control

First of all I am a beginner with C#. 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,

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!

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

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

But it looks weird. It shows the flip image and normal image both. Can someone help me on this? As I already said I am a beginner. Some simple code with explanation would be very helpful. Also can someone tell me what I am doing wrong?

Upvotes: 3

Views: 10613

Answers (1)

Wasif Hossain
Wasif Hossain

Reputation: 3950

DO NOT CREATE another Picture Box. You are seeing the original picture because you have not modified the original but the newly created one.

So the modified code is follows:

bool positionBird = true;

private void timer1_Tick(object sender, EventArgs e)
{
    if (PictureBox1.Location.X == Screen.PrimaryScreen.Bounds.Width)
    {
        positionBird = false;
        PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX); // picture flips only once when touches boundary
    }
    else if (PictureBox1.Location.X == 0)
    {
        positionBird = true;
        PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX); // picture flips only once when touches boundary
    }

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

Upvotes: 2

Related Questions