1

This seems so simple and I've done it before using the Image.RotateFlip method but this is for a predefined number of degrees (dependent upon how far the mouse has moved from its current position.

Here's what I have and I have no idea why it's not applying (the image doesn't move at all)

                using (var g = Graphics.FromImage(pbProfile.Image))
                {
                    g.TranslateTransform(pbProfile.Image.Width / 2f, pbProfile.Image.Height / 2f);
                    g.RotateTransform(Pixels);
                }
                pbProfile.Refresh();

I'm open to any and all suggestions as my brain is fried right now...

ANSWER

While the answer below is technically correct I found the actual solution to my problem here: Draw manipulated graphic into another graphic this explains that I don't need to be rotating the image I'm rotating but instead the graphic I'm writing it on.

                Bitmap bmp = new Bitmap(pbProfile.Image.Width, pbProfile.Image.Height);
                using (var g = Graphics.FromImage(bmp))
                {
                    g.TranslateTransform(bmp.Width / 2f, bmp.Height / 2f);
                    g.RotateTransform(Pixels);
                    g.DrawImage(pbProfile.Image, 0, 0);
                }
                pbProfile.Refresh();
Community
  • 1
  • 1
JasonSec
  • 614
  • 5
  • 12

2 Answers2

4

You're missing the g.DrawImage() call

David Crowell
  • 3,711
  • 21
  • 28
  • How would drawing an image to the picturebox help? What image would I draw? – JasonSec May 28 '14 at 17:32
  • You need to draw it to something. A bitmap maybe? – David Crowell May 28 '14 at 17:34
  • Indeed I tried this and pointed the picturebox image to it but the image doesnt change i'll throw that code up in a bit – JasonSec May 28 '14 at 17:44
  • The selected answer on this question: http://stackoverflow.com/questions/14184700/how-to-rotate-image-x-degrees-in-c Almost works. The size returned is a bit wonky. – David Crowell May 28 '14 at 18:10
  • The big problem was the fact it's a graphics object and outside of that object it doesn't appear to be applying to the image, drawimage takes an image as a parameter so you have to have the image rotated to draw to the picturebox or draw to a new bitmap to assign to the picturebox. – JasonSec May 28 '14 at 18:36
  • I accepted your answer and I appreciate your help I've noted additional information above for those interested. – JasonSec May 28 '14 at 19:35
1

Just a note remember that the parameter is in (float) degrees. You may get weird/unexpected results if "Pixels" is larger than 360.

*public void RotateTransform( float angle )

Parameters:angle

Type: System.Single

Angle of rotation in degrees.*

paymerich
  • 31
  • 5