Csharp/C Sharp/2D Graphics/Matrix

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

Matrix Demo

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Matrix_c
{
    public class Matrix1
    {
        [STAThread]
        static void Main(string[] args)
        {
          Matrix m = new Matrix();
          m.Rotate(90, MatrixOrder.Append);
          m.Translate(7, 12, MatrixOrder.Append);
          Point[] p = {new Point(20, 45)};
          Console.WriteLine(p.GetValue(0).ToString());
          m.TransformPoints(p);
          Console.WriteLine(p.GetValue(0).ToString());
    
          Console.ReadLine();
    }
    }
}


Matrix Draw

/*
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 MatrixDraw_c
{
  public class MatrixDraw : System.Windows.Forms.Form
    {
    internal System.Windows.Forms.HScrollBar rotate;
    internal System.Windows.Forms.VScrollBar xlate;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
    int XlateY;
    float Angle;
    Rectangle DrawingRect = new Rectangle(25, 25, 225, 225);

        public MatrixDraw()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
      Angle = 0;
      XlateY = 0;
      xlate.Minimum = -50;
      xlate.Maximum = 50;
      xlate.SmallChange = 1;
      xlate.LargeChange = 5;
      xlate.Value = 0;
      rotate.Minimum = -180;
      rotate.Maximum = 180;
      rotate.SmallChange = 1;
      rotate.LargeChange = 10;
      rotate.Value = 0;
      this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      this.SetStyle(ControlStyles.DoubleBuffer, true);
      this.SetStyle(ControlStyles.UserPaint, true);
    }
        /// <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()
        {
      this.rotate = new System.Windows.Forms.HScrollBar();
      this.xlate = new System.Windows.Forms.VScrollBar();
      this.SuspendLayout();
      // 
      // rotate
      // 
      this.rotate.Location = new System.Drawing.Point(8, 240);
      this.rotate.Name = "rotate";
      this.rotate.Size = new System.Drawing.Size(240, 16);
      this.rotate.TabIndex = 3;
      this.rotate.Scroll += new System.Windows.Forms.ScrollEventHandler(this.rotate_Scroll);
      // 
      // xlate
      // 
      this.xlate.Location = new System.Drawing.Point(264, 32);
      this.xlate.Name = "xlate";
      this.xlate.Size = new System.Drawing.Size(16, 200);
      this.xlate.TabIndex = 2;
      this.xlate.Scroll += new System.Windows.Forms.ScrollEventHandler(this.xlate_Scroll);
      // 
      // MatrixDraw
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.rotate,
                                                                  this.xlate});
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "MatrixDraw";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "MatrixDraw";
      this.Load += new System.EventHandler(this.MatrixDraw_Load);
      this.ResumeLayout(false);
    }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new MatrixDraw());
        }
    private void MatrixDraw_Load(object sender, System.EventArgs e)
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics G  = e.Graphics;
      G.SmoothingMode = SmoothingMode.AntiAlias;
      // Create a graphics path, add a rectangle, set colors
      GraphicsPath Path = new GraphicsPath();
      Path.AddRectangle(new Rectangle(75, 100, 100, 75));
      PointF[] Pts  = Path.PathPoints;
      PathGradientBrush B = new PathGradientBrush(Pts);
      B.CenterColor = Color.Aqua;
      Color[] SColor = {Color.Blue};
      B.SurroundColors = SColor;
      //We will translate the brush!  NOT the rectangle!
      Matrix m = new Matrix();
      m.Translate(0, XlateY, MatrixOrder.Append);
      m.RotateAt(Angle, B.CenterPoint, MatrixOrder.Append);
      B.MultiplyTransform(m, MatrixOrder.Append);
      G.FillRectangle(B, DrawingRect);
      base.OnPaint(e);
      m.Dispose();
      B.Dispose();
      Path.Dispose();
    }
    private void xlate_Scroll(object sender, 
                              System.Windows.Forms.ScrollEventArgs e)
    {
      XlateY = xlate.Value;
      this.Invalidate(DrawingRect);
    }
    private void rotate_Scroll(object sender, 
                               System.Windows.Forms.ScrollEventArgs e)
    {
      Angle = rotate.Value;
      this.Invalidate(DrawingRect);
    }
    }
}


new Matrix(, , -, , 0, 0)

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class MobyDick: Form
{
     public static void Main()
     {
          Application.Run(new MobyDick());
     }
     public MobyDick()
     {
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          grfx.Transform = new Matrix(0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
          grfx.DrawString("abc", Font, new SolidBrush(clr), 
                          new Rectangle(0, 0, cx, cy));
     }
}


Text direction (Matrix Rotate)

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = CreateGraphics();
        string txt = "HELLO";
        float alpha = 45.0f;
        int fontSize = 24;
        Point center = new Point(90, 20);
        FontFamily ff = new FontFamily("Times New Roman");
        Font f = new Font(ff, fontSize, FontStyle.Regular);
        StringFormat sf = new StringFormat();
        sf.FormatFlags = StringFormatFlags.DirectionVertical;
        g.DrawString(txt, f, new SolidBrush(Color.Blue), center, sf);
        g.TranslateTransform(center.X, center.Y);
        g.DrawEllipse(Pens.Magenta, new Rectangle(0, 0, 1, 1));
        GraphicsPath gp = new GraphicsPath();
        gp.AddString(txt, ff, (int)FontStyle.Bold, fontSize + 4, new Point(0, 0), sf);
        Matrix m = new Matrix();
        m.Rotate(alpha);
        gp.Transform(m);
        g.DrawPath(Pens.Red, gp);
        g.RotateTransform(-alpha);
        g.DrawString(txt, f, new SolidBrush(Color.Black), 0, 0, sf);
        gp.Dispose(); g.Dispose(); m.Dispose();
    }
}