GDI+学习笔记3-坐标系和坐标变换

来源:互联网 发布:淘宝摩托车配件 编辑:程序博客网 时间:2024/05/16 18:16
GDI+提供了两张坐标转换方式:世界坐标转换和页面坐标转换,你可以对所绘制图形作旋转,缩放,平移等操作。
两种坐标变换还运行您使用多种坐标系。

GDI+使用三种坐标系:世界坐标、页面坐标和设备坐标.
世界坐标是用于建立特殊图形世界模型的坐标系,也是在 .NET Framework 中传递给方法的坐标系。
页面坐标系是指绘图图面(如窗体或控件)使用的坐标系。
设备坐标系是在其上进行绘制的物理设备(如屏幕或纸张)所使用的坐标系。
当调用 myGraphics.DrawLine(myPen, 0, 0, 160, 80) 时,传递给 DrawLine 方法的点( (0, 0) 和 (160, 80))位于世界坐标空间内。在GDI+将直线绘制到屏幕之前,坐标将经过一系列转换。一种称为“世界变换”的变换可将世界坐标转换为页面坐标,而另一种称为“页面变换”的变换可将页面坐标转换为设备坐标。

世界坐标系是相对于坐标原点的,
页面坐标系原点永远都位于屏幕的左上角,页面坐标的单位是可以改变的。
设备坐标系的坐标单位永远都是像素

用于将世界坐标映射到页面坐标的世界变换保存在 Graphics 类的 Transform 属性中。
myGraphics.TranslateTransform(100, 50);
  myGraphics.DrawLine(myPen, 0, 0, 160, 80);
Graphics类中变换方法有: 
TranslateTransform(), 
RotateTransform(), 
ScaleTransform(), 
ResetTransform(), 
MultiplyTransform(Matrix), 
MultiplyTransform(Matrix, MatrixOrder)

页面变换将页面坐标映射到设备坐标。Graphics 类提供了用于操作页面变换的 PageUnit 和 PageScale 属性。 Graphics 类还提供了两个只读属性: DpiX 和 DpiY,用于检查显示设备每英寸的水平点数和垂直点数。 
myGraphics.PageUnit = GraphicsUnit.Inch;
  myGraphics.DrawLine(myPen, 0, 0, 2, 1);

可使用 Graphics 类的 PageUnit 属性指定除像素以外的其他度量单位。 

示例1代码为将将世界坐标原点转换到窗体客户区的左下角,并且将Y轴正方向朝上:

Pen myPen = new Pen(Color.Black);
        Rectangle rect = this.ClientRectangle;
        // 将世界坐标原点转换到窗体客户区的左下角
        e.Graphics.TranslateTransform(0, rect.Height);
        e.Graphics.ScaleTransform(1, -1);
        e.Graphics.DrawLine(myPen, 0, 0, 160, 180);
        myPen.Dispose();

示例2代码为世界坐标转换和页面坐标转换例子: 

        // ChangePageScaleAndTranslateTransform
        private void CoordinateTransform(PaintEventArgs e)
        {
            Rectangle rect1 = new Rectangle(20, 20, 50, 100);
            e.Graphics.DrawRectangle(Pens.SlateBlue, rect1);
            e.Graphics.PageScale = 2.0F;
            e.Graphics.TranslateTransform(10.0F, 20.0F);
            e.Graphics.DrawRectangle(Pens.Tomato, rect1);
            e.Graphics.PageScale = 1.0F;
            e.Graphics.ResetTransform();
            SolidBrush transparentBrush = new SolidBrush(Color.FromArgb(50, Color.Yellow));
            Rectangle rect2 = new Rectangle(60, 60, 100, 200);
            e.Graphics.FillRectangle(transparentBrush, rect2);
        }


坐标全局变换与局部变换示例:

 private void GlobalAndLocalTransform(PaintEventArgs e)
        {
            Graphics myGra = e.Graphics;
            Pen myPen = Pens.Violet;
            //myGra.DrawEllipse(myPen, 0, 0, 100, 50);
            // 全局变换
            //myGra.ScaleTransform(1, 0.5f);
            //myGra.TranslateTransform(50, 0, MatrixOrder.Append);
            //myGra.RotateTransform(30, MatrixOrder.Append);
            //myGra.DrawEllipse(myPen, 0, 0, 100, 50);


            // 局部变换
            Matrix matrix = new Matrix();
            matrix.Rotate(45);
            GraphicsPath graphicPath = new GraphicsPath();
            Rectangle rect = new Rectangle(100, 50, 150, 100);
            graphicPath.AddRectangle(rect);
            graphicPath.Transform(matrix);
            myGra.DrawRectangle(myPen,rect);
            myGra.DrawPath(myPen, graphicPath);
        }

0 0
原创粉丝点击