Rotate
Hi all,
first let me say that I'm a huge fan of this website I scan it's tutorial section on a dailey bases and I have found it to be of great help to me. But I'm stuck on a certain problem using the gdi+ graphics class.
In c#, I have drawn a figure using a graphicsPath, now when I want to rotate this figure, it does this using 0,0 as the pivoting point. But this is not what I would like, I would like to set the pivot point to the center of the figure. I have googled and found a few articles about this but none of these I understand fully.
Edwin BlommaertsPosted Apr 1, 2007, 8:22 AM
Richard BlythePosted Mar 30, 2007, 12:52 AM
GraphicsPath gp = null; Matrix myRotateMatrix = null; protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.Clear(Color.White); //ensure the graphicsPath object is created
if (gp == null) CreateGraphicsPathObject(); //draw the gp object
e.Graphics.FillPath(Brushes.Blue, gp);
}
private void btnRotate_Click(object sender, EventArgs e){
//transform (in this case the transformation is a rotation) the//graphics path object by the amount specified by myRotateMatrix
gp.Transform(myRotateMatrix); //Tell the Form that it needs to repaint itself
this.Invalidate();
}
private void CreateGraphicsPathObject(){
gp =
new GraphicsPath();StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
gp.AddString("Click To Rotate Me!", this.Font.FontFamily, 1, 20, new Point(50, 100), sf); //Since we are creating the GraphicsPath object
//we need to initialze our matrix object that will
//control the rotation
myRotateMatrix = new Matrix(); PointF myCenterPoint = gp.GetBounds().Location;
myCenterPoint.X = myCenterPoint.X + gp.GetBounds().Width / 2;
myCenterPoint.Y = myCenterPoint.Y + gp.GetBounds().Height / 2;
myRotateMatrix.RotateAt(20F, myCenterPoint);
}