图形变换
用于变换的方法:
Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); //first,draw a non-scaled rectangle and circle g.DrawRectangle(Pens.Black, 10, 10, 50, 50); g.DrawEllipse(Pens.Black,10, 10, 10, 10); //now apply the scaling transformation //this will scale subsequent operations by 2x in horizontally //and 3x vertically g.ScaleTransform(2.0f, 3.0f); //now drawthe same rectangle and circle //but with the scaliing transformation g.DrawRectangle(Pens.Black, 10, 10, 50, 50); g.DrawEllipse(Pens.Black, 10, 10, 10, 10);
头两次绘图操作向绘图表面绘制了一个正方形和一个圆,当调用这两次操作时,没有指定变换,所以GDI+采用的是默认的通用变换--把通用坐标系映射到页坐标系,映射通用坐标系中的(0,0)到页坐标系中的(0,0),不应用缩放、平移或者错位变换,后两次应用了缩放的效果,注意:同时还缩放了用来绘制图形的钢笔的宽度。
平移:
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Font f = new Font("Times New Roman", 24); //draw text to the surface g.DrawString("Translation", f, Brushes.Black, 0, 0); //translate the text 150 pixels horizontally and 75 vertically g.TranslateTransform(150, 75); //draw the translated text g.DrawString("Translation", f, Brushes.Black, 0, 0); }
累积变换
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); for (int i = 1; i <= 5; ++i) { //first,draw a rectangle with the current translation g.DrawRectangle(Pens.Black, 10, 10, 30, 50); //add a translation to the existing transformation g.TranslateTransform(10, 20); }
如果想要应用非累积变换,那么我们可以调用ResetTransform方法。
旋转变换
旋转变换执行一次顺时针方向的旋转,以度为单位,旋转的中心是坐标系的当前原点。
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Font f = new Font("Times New Roman", 24); //draw text to thesurface g.DrawString("Rotation", f, Brushes.Black, 0, 0); //rotate the text through 45 degrees g.RotateTransform(45); //draw the rotated text g.DrawString("Rotation", f, Brushes.Black, 0, 0); }
一个小例子:
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Font f = new Font("Times New Roman", 24); for (float angle = 0; angle < 360; angle += 45) { g.ResetTransform(); g.TranslateTransform(ClientRectangle.Width / 2, ClientRectangle.Height / 2); g.RotateTransform(angle); g.DrawString("Hello,World!", f, Brushes.Black, 50, 0); } }
private void Form1_Load(object sender, EventArgs e) { this.ResizeRedraw = true; }
注:沿着x轴缩放一个负值,结果是y轴的一个反射;沿着y轴缩放一个负值,结果是x抽的一个反射。
错位变换
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Matrix m = new Matrix(); //the following method call sets up the shear in the matrix //the first argument is the shear factor in the x direction //the secong argument is the shear factor in the y direction m.Shear(.6f, 0); g.DrawRectangle(Pens.Black, 10, 10, 50, 50); g.MultiplyTransform(m); g.DrawRectangle(Pens.Black, 70, 10, 50, 50); }