Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

C# Image/PictureBox Rotations

Rate me:
Please Sign up or sign in to vote.
4.62/5 (25 votes)
16 Feb 2010CPOL1 min read 262.4K   20.7K   36  
How to rotate any image from the center of that image
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RotatePictureBox
{
    public partial class Form1 : Form
    {
        private Bitmap image = null;
        private float angle = 0.0f;

        public Form1()
        {
            InitializeComponent();
            angleNumericUpDown.Value = (Decimal)angle;
        }

        //Load image
        private void LoadImageBtn_Click(object sender, EventArgs e)
        {
            lfd.InitialDirectory = Application.StartupPath;
            lfd.ShowDialog();
        }

        private void lfd_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                image = new Bitmap(lfd.FileName);

                pictureBox1.Image = (Bitmap)image.Clone();
                ImagePathTxtBox.Text = lfd.FileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            RotateImage(pictureBox1, image, angle);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    RotateImage(pictureBox1, image, angle++);
                    break;
                case Keys.Down:
                    RotateImage(pictureBox1, image, angle--);
                    break;
                case Keys.Right:
                    RotateImage(pictureBox1, image, angle++);
                    break;
                case Keys.Left:
                    RotateImage(pictureBox1, image, angle--);
                    break;
            }
        }

        private void angleNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            angle = (float)angleNumericUpDown.Value;
            RotateImage(pictureBox1, image, angle);
        }

        private void RotateImage(PictureBox pb, Image img, float angle)
        {
            if (img == null || pb.Image == null)
                return;

            Image oldImage = pb.Image;
            pb.Image = Utilities.RotateImage(img, angle);
            if (oldImage != null)
            {
                oldImage.Dispose();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions