简体   繁体   中英

Rotate image in c#

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();
    }
}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM