0

I wish the image in picture box could flip when key left is pressed, as the code shown below, but it didn't flip when key left is pressed. Anyone could help ? Thanks a lot!

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        KeyDown += new KeyEventHandler(Form1_KeyDown);
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;

        if (e.KeyCode == Keys.Right)
        {
            x += 10;
        }

        else if (e.KeyCode == Keys.Left)
        {
            pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
            pictureBox1.Refresh();
            x -= 10;
        }

        else if (e.KeyCode == Keys.Up)
        {
            y -= 10;
        }

        pictureBox1.Location = new Point (x, y);

        pictureBox1.Invalidate();
    }
}
}
J.y
  • 1
  • 1
  • Having tested the code you have up here, it seems to work fine - are you using the arrows on the numbad or the actual arrow keys? as they do map to different a Keys enum? – Gibbon May 04 '18 at 12:25
  • You need to __reassign__ to the Image field as in the [example](https://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip(v=vs.110).aspx) – TaW May 04 '18 at 12:36
  • I already tested it, with the actual arrow keys , the picturebox will move but the image didn't flip as key left is pressed. – J.y May 04 '18 at 12:39
  • Suggestion: What version if .NET are you using? If it's 3.5 or greater then I highly suggest switching to ```WPF``` or adding an ```ElementHost``` to your ```Form``` and adding the ```XAML``` content into the ```ElementHost```. The only reason I'm saying this is you can easily rotate and do so much more with it than you can in WinForms. – Michael Puckett II May 04 '18 at 13:16
  • 4.7.1, the project have to be done by using c#. Anyway, appreciate the suggestion given =) Thank you ! – J.y May 04 '18 at 14:40

1 Answers1

-1

Instead of Image you are trying rotating the picture box. As per the example given in the link. rotate the image then assign it to picture box.

Refer this link officially from Microsoft docs https://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip(v=vs.110).aspx

Mohammed Gadi
  • 381
  • 5
  • 18
  • 1
    In his code he is calling picturebox.image.rotateflip(), so he is directly flipping the image and not the picturebox – Gibbon May 04 '18 at 12:22