First of all, it's a really bad idea to rotate anything in
PictureBox
. This control is not designed for anything even minimally complicated. You can either do graphical conversion on a
System.Drawing.Bitmap
, or, if your real goal is showing something on screen, render it immediately by overriding the virtual method
System.Windows.Control.OnPaint
or handling the event
System.Windows.Control.Paint
and using the instance of
System.Drawing.Graphics
passed to you in even arguments. If you use
PictureBox
, it does not help you even a bit, but only creates extra hassles and consumes extra resources and CPU time. Don't do it.
For further detail on that, please see my past answers:
Append a picture within picturebox[
^],
draw a rectangle in C#[
^],
How do I clear a panel from old drawing[
^].
See also:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
How to speed up my vb.net application?[
^],
capture the drawing on a panel[
^],
Drawing Lines between mdi child forms[
^].
You can even do both bitmap and on-screen rendering in one method, which is the best approach if you need to do both independently. You only need to write a separate method with the parameter of the type
System.Drawing.Graphics
. In one call, the instance of the
Graphics
will be from pain event arguments, in another call, from an instance of
Bitmap
.
Now, what to do with rotation, as well as other transformations? What you do can work, but you are doing it in a hard way. Many basic problems like that are already solved for you in the
System.Drawing.Graphics
. This is reliably done via the
Transform
which is of the type
System.Drawing.Drawing2D.Matrix
. You can start from the unit matrix (doing identical transform) and modify it according to a set of transforms you need. This type provides you with predefined operations for rotations, shearing, translation, scaling, reflections, as well as the custom matrix transforms.
Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx[
^].
This is easy to do and fun to try, not as boring and error-prone as your trivial ad-hoc linear algebra operations in linear metrics space. Someone else already done most of the job. :-)
—SA