Bootstrap

GDI+入门(八、坐标系的变化与图形变换

八、坐标系的变化与图形变换

private void Form1_Paint(object sender, PaintEventArgs e)

        {

            Graphics g = e.Graphics;

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            g.DrawRectangle(Pens.Black, 10, 10, 50, 50);

            g.DrawEllipse(Pens.Black,10,10,10,10);

            g.ScaleTransform(2.0f, 3.0f);

            g.DrawRectangle(Pens.Black, 10, 10, 50, 50);

            g.DrawEllipse(Pens.Black, 10, 10, 10, 10);

            g.ScaleTransform(0.5f, 0.3333333f);

            g.DrawRectangle(Pens.Red, 20, 30, 100, 150);

            g.DrawEllipse(Pens.Red, 20, 30, 20, 30);

        }

        private void button1_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            Font f = new Font("Times New Roman", 24);

            g.DrawString("Traslation",f,Brushes.Black,0,0);

            g.TranslateTransform(150, 75);

            g.DrawString("Traslation", f, Brushes.Black, 0, 0);

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            for (int i = 1; i <= 5; ++i)

            {

                g.DrawRectangle(Pens.Black, 10, 10, 30, 50);

                g.TranslateTransform(2, 10);

            }

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            g.DrawEllipse(Pens.Black, 20, 20, 30, 50);

            g.TranslateTransform(-15, 0);

            g.DrawEllipse(Pens.Black, 20, 20, 30, 50);

            g.ResetTransform();

            g.TranslateTransform(0, 30);

            g.DrawEllipse(Pens.Black, 20, 20, 30, 50);

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            Font f = new Font("Times New Roman", 24);

            g.DrawString("Rotation", f, Brushes.Black, 0, 0);

            g.RotateTransform(45);

            g.TranslateTransform(100, 10);

            g.DrawString("Rotation", f, Brushes.Black, 0, 0);

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            Font f = new Font("Times New Roman", 16);

            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.Red, 50, 0);

            }

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            Font f = new Font("Times New Roman", 24);

            g.TranslateTransform(175, 50);

            g.DrawString("BOBUI.DH", f, Brushes.Black, 0, 0);

            g.ScaleTransform(-1, 1);

            g.DrawString("BOBUI.DH", f, Brushes.Black, 0, 0);

        }

 

        private void button7_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            Matrix m = new Matrix();

            m.Shear(0.6f, 0);

            g.DrawRectangle(Pens.Black, 10, 10, 50, 50);

            g.MultiplyTransform(m);

            g.DrawRectangle(Pens.Black, 70, 10, 50, 50);

        }

Form1_Paint:这里里面实现了一个图形的放缩

ScaleTransform:将指定的缩放操作应用于此 Graphics 的变换矩阵,方法是将该对象的变换矩阵左乘该缩放矩阵

 

button1_Click:坐标系平移实现图像位置的变化

TranslateTransform:通过使此 Graphics 的变换矩阵左乘指定的平移来更改坐标系统的原点。

button2_Click:坐标原点的变换,这个例子说明TranslateTransform变换坐标是连续的

button3_Click:还原系统原点

ResetTransform:将此 Graphics 的世界变换矩阵重置为单位矩阵

button4_Click:图像的旋转

RotateTransform:指定旋转应用于此 Graphics 的变换矩阵

button5_Click:坐标系变换与旋转综合示例

button6_Click:图形的变换

ScaleTransform:指定的缩放操作应用于此 Graphics 的变换矩阵,方法是将该对象的变换矩阵左乘该缩放矩阵

button7_Click:图形的变换

MultiplyTransform:将此 Graphics 的世界变换乘以指定的 Matrix

Shear:通过预先计算切变向量将指定的切变向量应用到此 Matrix

本人也在学习GDI+,写得比较简单,让高手见笑了。欢迎高手给我指点

邮箱:[email protected]

QQ125941562

 

;