Csharp/C Sharp/2D Graphics/Transform — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:42, 26 мая 2010

Add a translation to the existing transformation

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, this.ClientRectangle);
      for (int i = 1; i <= 10; ++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(20, 10);
      }
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Reset the transformation

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, ClientRectangle);
      g.DrawEllipse(Pens.Black, 20, 20, 30, 50);
      // Translate by -15 pixels in horizontal direction
      g.TranslateTransform(-15, 0);
      g.DrawEllipse(Pens.Red, 20, 20, 30, 50);
      // Reset the transformation
      // Translate by 30 pixels in vertical direction
      g.ResetTransform();
      g.TranslateTransform(0, 30);
      g.DrawEllipse(Pens.Blue, 20, 20, 30, 50);
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Rotate Transform

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Drawing.Text;
namespace GDI_Basics
{
    /// <summary>
    /// Summary description for RotateTransform.
    /// </summary>
    public class RotateTransform : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public RotateTransform()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // RotateTransform
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "RotateTransform";
            this.Text = "RotateTransform";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.RotateTransform_Paint);
        }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new RotateTransform());
        }
        private void RotateTransform_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            // Optimize text quality.
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            // Move origin to center of form so we can rotate around that.
            e.Graphics.TranslateTransform(this.Width / 2 - 30, this.Height / 2 - 30);
            DrawText(e.Graphics);
            e.Graphics.RotateTransform(45);
            DrawText(e.Graphics);
            e.Graphics.RotateTransform(75);
            DrawText(e.Graphics);
            e.Graphics.RotateTransform(160);
            DrawText(e.Graphics);
        }
        private void DrawText(Graphics g)
        {
            g.DrawString("Text", new Font("Verdana", 30, FontStyle.Bold), 
                Brushes.Black, 0, 10);
        }
    }
}


Shape and Transform

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace GraphicsDraw_c
{
    /// <summary>
    /// Summary description for GraphicsDraw.
    /// </summary>
    public class GraphicsDraw : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public GraphicsDraw()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      // 
      // GraphicsDraw
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(504, 629);
      this.Name = "GraphicsDraw";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "GraphicsDraw";
      this.Load += new System.EventHandler(this.GraphicsDraw_Load);
    }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new GraphicsDraw());
        }
    private void GraphicsDraw_Load(object sender, System.EventArgs e)
    {
    
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      Rectangle R1 = new Rectangle(10, 10, 40, 40);
      e.Graphics.SmoothingMode=SmoothingMode.HighQuality;
      e.Graphics.DrawRectangle(Pens.Black,R1);
      e.Graphics.TranslateTransform(50.0F, 0.0F);
      e.Graphics.FillRectangle(Brushes.Black,R1);
      //Draw three rectangles
      Rectangle[] ThreeRects = {new Rectangle(110, 10, 40, 40), 
                                new Rectangle(160, 10, 40, 40), 
                                new Rectangle(210, 10, 40, 40)};
      e.Graphics.ResetTransform();
      e.Graphics.DrawRectangles(Pens.Red, ThreeRects);
      //Draw three filled rectangles
      e.Graphics.ResetTransform();
      e.Graphics.TranslateTransform(100.0F, 0.0F);
      e.Graphics.FillRectangles(Brushes.Red, ThreeRects);

      //Use first rect to bound ellipse as circle
      e.Graphics.ResetTransform();
      e.Graphics.TranslateTransform(0.0F, 50.0F);
      e.Graphics.DrawEllipse(Pens.Green,R1);
      //Draw a filled ellipse
      e.Graphics.TranslateTransform(50.0F, 0.0F);
      e.Graphics.FillEllipse(Brushes.Green,R1);
      //Use first rect to bound pie 
      e.Graphics.ResetTransform();
      e.Graphics.TranslateTransform(100.0F, 50.0F);
      e.Graphics.DrawPie(Pens.DarkViolet, R1, 0, 60);
      //Use first rect to fill pie 
      e.Graphics.ResetTransform();
      e.Graphics.TranslateTransform(150.0F, 50.0F);
      e.Graphics.FillPie(Brushes.DarkViolet, R1, 0, 60);
      //Use first rect to bound arc 
      e.Graphics.ResetTransform();
      e.Graphics.TranslateTransform(200.0F, 50.0F);
      e.Graphics.DrawArc(Pens.DarkBlue, R1, 40, 160);
      PointDraw ( e.Graphics );
      PathDraw ( e.Graphics );
   }
    private void PointDraw( Graphics G )
    {
      //Start with clean slate
      G.ResetClip();
      G.ResetTransform();
      //Separate sections
      G.DrawLine(Pens.Black,10,110,this.Width-10,110);
      //------------ Draw Line -----------------------
      //Generate start and end points
      Point StartPt = new Point(10,130);
      Point EndPt = new Point(200,130);
      Pen P = new Pen(Brushes.CadetBlue, 5);
      G.DrawLine(P, StartPt, EndPt);
      //------------- Draw lines ----------------------
      //Translate in the Y Direction
      Size Xlate_Y = new Size(0,40);
      //Translate in the X Direction
      Size Xlate_X = new Size(200,0);
      Point Pt = StartPt;
      //Generate set of points based on offsets of original point
      Point[] ManyPoints = { (Pt + Xlate_X), 
                             (Pt = Pt + Xlate_X + Xlate_Y), 
                             (Pt = Pt + Xlate_X) };
      P.Color=Color.Firebrick;
      G.DrawLines(P, ManyPoints);
      //------------ DrawBezier and Polygon -------------------
      StartPt.X=10;
      StartPt.Y=250;
      Point CtlPtA = new Point(50,150);
      Point CtlPtB = new Point(350,300);
      EndPt.X=400;
      EndPt.Y=250;
      Point[] PolyPoints = { StartPt, CtlPtA, EndPt, CtlPtB };
      //Draw the controlling shape of the Bezier spline
      G.DrawPolygon ( Pens.DarkSeaGreen, PolyPoints );
      //Draw the actual Spline
      P.Color=Color.DarkSeaGreen;
      P.Width=3;
      G.DrawBezier( P, StartPt, CtlPtA, CtlPtB, EndPt );
      //---------- Draw two Bezier splines ---------------------
      Size Y = new Size(0,40);
      Size X = new Size(20,0);
      //Y Translated start of first spline
      //Same control points for first spline,
      //X,Y Translated end of first spline, 
      //X Translate control points for second spline,
      //X,Y New end point for second spline
      Point[] TwoSplines = { StartPt+Y, 
                             CtlPtA, 
                             CtlPtB, 
                             EndPt+Y-new Size(200,0), 
                             CtlPtA+X, 
                             CtlPtB+X, 
                             EndPt+Y+X };
      P.Color=Color.Gold;
      G.DrawBeziers (P, TwoSplines);
      //---------- Draw a closed curve -----------
      PolyPoints[0] = new Point(100, 350);
      PolyPoints[1] = new Point(250, 300);
      PolyPoints[2] = new Point(250, 400);
      PolyPoints[3] = new Point(150, 400);
      P.Color=Color.Olive;
      //Curve traces outside of polygon
      //Curve is closed cardinal spline & hits all points
      G.DrawPolygon (P, PolyPoints);
      G.DrawClosedCurve(P,PolyPoints);
      //Uncomment next line to fill the egg shape
      // G.FillClosedCurve(Brushes.AliceBlue,PolyPoints);
      //---------- Draw an open cardinal curve -----------
      Point[] CardPoints = { new Point( 310, 350 ),
                             new Point( 330, 360 ),
                             new Point( 360, 320 ),
                             new Point( 390, 370 ),
                             new Point( 400, 350 ),
                             new Point( 480, 340 )};
      P.Color=Color.DarkOrange;
      G.DrawCurve(P, CardPoints);
      P.Dispose();
    }
    private void PathDraw( Graphics G )
    {
      //Start with clean slate
      G.ResetClip();
      G.ResetTransform();
      //Separate sections
      G.DrawLine(Pens.Black, 10, 420, this.Width-10, 420);
      //Make a blank path and add shapes to it
      GraphicsPath gp = new GraphicsPath();
      Pen P = new Pen(Brushes.ForestGreen,3);
      gp.AddRectangle(new Rectangle(10, 450, 100, 100));
      gp.AddEllipse(120, 450, 100, 100);
      gp.AddPie ( 70, 500, 100, 100, 25, 120 );
      //Draw the outline of the path and fill it in
      G.DrawPath (P, gp );
      G.FillPath(Brushes.Bisque,gp);
      P.Dispose();
    }
    }
}


Translate the text 150 pixels horizontally and 75 vertically

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.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("www.nfex.ru", f, Brushes.Black, 0, 0);
      // Translate the text 150 pixels horizontally and 75 vertically
      g.TranslateTransform(150, 75);
      // Draw the translated text
      g.DrawString("www.nfex.ru", f, Brushes.Black, 0, 0);
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Translate Transform

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace GDI_Basics
{
    /// <summary>
    /// Summary description for TranslateTransform.
    /// </summary>
    public class TranslateTransform : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public TranslateTransform()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // TranslateTransform
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "TranslateTransform";
            this.Text = "TranslateTransform";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.TranslateTransform_Paint);
        }
        #endregion
        private void TranslateTransform_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            // Draw several squares in different places.
            DrawRectangle(e.Graphics);
            e.Graphics.TranslateTransform(180, 60);
            DrawRectangle(e.Graphics);
            e.Graphics.TranslateTransform(-50, 80);
            DrawRectangle(e.Graphics);
            e.Graphics.TranslateTransform(-100, 50);
            DrawRectangle(e.Graphics);
        }
        private void DrawRectangle(Graphics g)
        {
            Pen drawingPen = new Pen(Color.Red, 30);
            // Draw a rectangle at a fixed position.
            g.DrawRectangle(drawingPen, new Rectangle(20, 20, 20, 20));
        }

        [STAThread]
        static void Main() 
        {
            Application.Run(new TranslateTransform());
        }
    }
}


Translate Transform three times

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
public class Form1 : Form
{
      public Form1() {
            InitializeComponent();
      }
    private void SimpleStyleRenderer_Paint(object sender, PaintEventArgs e)
    {
      e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
      DrawRectangle(e.Graphics);
      e.Graphics.TranslateTransform(180, 60);
      DrawRectangle(e.Graphics);
      e.Graphics.TranslateTransform(-50, 80);
      DrawRectangle(e.Graphics);
      e.Graphics.TranslateTransform(-100, 50);
      DrawRectangle(e.Graphics);
    }
    private void DrawRectangle(Graphics g)
    {
      Pen drawingPen = new Pen(Color.Red, 30);
      g.DrawRectangle(drawingPen, new Rectangle(20, 20, 20, 20));
    }
    private void InitializeComponent()
    {
      this.SuspendLayout();
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(600, 600);
      this.Name = "SimpleStyleRenderer";
      this.Text = "SimpleStyleRenderer";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleStyleRenderer_Paint);
      this.ResumeLayout(false);
    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }
}