Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

C# Image/PictureBox Rotations

, 16 Feb 2010 CPOL
How to rotate any image from the center of that image
RotatePictureBox.zip
RotatePictureBox.exe
RotatePictureBoxSource.zip
RotatePictureBox
RotatePictureBox
bin
Debug
Release
obj
Debug
Refactor
RotatePictureBox.csproj.GenerateResource.Cache
RotatePictureBox.exe
RotatePictureBox.Form1.resources
RotatePictureBox.pdb
RotatePictureBox.Properties.Resources.resources
TempPE
Release
RotatePictureBox.csproj.GenerateResource.Cache
RotatePictureBox.exe
RotatePictureBox.Form1.resources
RotatePictureBox.pdb
RotatePictureBox.Properties.Resources.resources
TempPE
Properties
Settings.settings
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)

Share

About the Author

MusicMonkey5555

United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 16 Feb 2010
Article Copyright 2010 by MusicMonkey5555
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid