Csharp/C Sharp/2D Graphics/Rotate

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

Rotate by angles

<source lang="csharp">

 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", 16);
     for (float angle = 0; angle < 360; angle += 45)
     {
       g.ResetTransform();
       g.TranslateTransform(ClientRectangle.Width / 2,
                  ClientRectangle.Height / 2);
       g.RotateTransform(angle);
       g.DrawString("www.nfex.ru", f, Brushes.Black, 50, 0);
     }
   }
   private void Form1_Resize(object sender, System.EventArgs e)
   {
     Invalidate();
   }
 }


      </source>


Rotate the text 45 degrees

<source lang="csharp">

 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, 50, 50);
     // Rotate the text through 45 degrees
     g.RotateTransform(45);
     // Draw the rotated text
     g.DrawString("www.nfex.ru", f, Brushes.Black, 150, 50);
   }
   private void Form1_Resize(object sender, System.EventArgs e)
   {
     Invalidate();
   }
 }


      </source>


Rotate Transform and Translate Transform

<source lang="csharp"> 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;
     e.Graphics.TranslateTransform(this.Width / 2 - 100, this.Height / 2 - 100);
     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("www.nfex.ru", new Font("Verdana", 30, FontStyle.Bold),
       Brushes.Black, 0, 0); 
   }
   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());
     }

}


      </source>


Translate and Rotate the string

<source lang="csharp">

 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);
     g.DrawString("www.nfex.ru", f, Brushes.Black, 0, 0);
     g.TranslateTransform(150, 0);
     g.RotateTransform(45);
     // Draw the transformed text
     g.DrawString("www.nfex.ru", f, Brushes.Black, 0, 0);
   }
   private void Form1_Resize(object sender, System.EventArgs e)
   {
     Invalidate();
   }
 }
          
      </source>