I was working on an Etch A Sketch like program and made a knob image and wanted to simply rotate it as the user hit the arrow keys. I found many sources online, but none that got it done just how I wanted (see links below in References section). I then decided since I figured it out and got it all working why not make a simple demo with source that others could use.
- Loading an image from a dialogue prompt
- Creating bitmaps, image/draw updates, and Graphic manipulation
Using the Code
There is a class called
Utilities in the Utilities.cs file that contains the functions for rotating an image from the center or given an offset to rotate from.
Here is the main rotation function
public static Bitmap RotateImage(Image image, PointF offset, float angle)
if (image == null)
throw new ArgumentNullException("image");
Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
Graphics g = Graphics.FromImage(rotatedBmp);
g.DrawImage(image, new PointF(0, 0));
And here is how it is used:
Image image = new Bitmap("Picture.png");
pictureBox.Image = (Bitmap)image.Clone();
Image oldImage = pictureBox.Image;
pictureBox.Image = Utilities.RotateImage(image, 35);
if (oldImage != null)
Points of Interest
Graphics are very powerful and allow you to do image manipulation really easily. For instance, this code could easily be modified to scale instead. I may added to this with more utilities like that.
- 16th February, 2010: Initial post
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.