Drawing Squares

image_pdfimage_print


   

/*
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.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DrawingSquares
{
    /// <summary>
    /// Summary description for DrawingSquares.
    /// </summary>
    public class DrawingSquares : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ContextMenu mnuForm;
        internal System.Windows.Forms.MenuItem mnuNewSquare;
        internal System.Windows.Forms.ContextMenu mnuLabel;
        internal System.Windows.Forms.MenuItem mnuColorChange;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DrawingSquares()
        {
            //
            // 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()
        {
            this.mnuForm = new System.Windows.Forms.ContextMenu();
            this.mnuNewSquare = new System.Windows.Forms.MenuItem();
            this.mnuLabel = new System.Windows.Forms.ContextMenu();
            this.mnuColorChange = new System.Windows.Forms.MenuItem();
            // 
            // mnuForm
            // 
            this.mnuForm.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                    this.mnuNewSquare});
            // 
            // mnuNewSquare
            // 
            this.mnuNewSquare.Index = 0;
            this.mnuNewSquare.Text = "Create New Square";
            this.mnuNewSquare.Click += new System.EventHandler(this.mnuNewSquare_Click);
            // 
            // mnuLabel
            // 
            this.mnuLabel.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.mnuColorChange});
            // 
            // mnuColorChange
            // 
            this.mnuColorChange.Index = 0;
            this.mnuColorChange.Text = "Change Color";
            this.mnuColorChange.Click += new System.EventHandler(this.mnuColorChange_Click);
            // 
            // DrawingSquares
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(628, 426);
            this.ContextMenu = this.mnuForm;
            this.Name = "DrawingSquares";
            this.Text = "DrawingSquares";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawingSquares_MouseDown);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new DrawingSquares());
        }

        private void mnuNewSquare_Click(object sender, System.EventArgs e)
        {
            // Create and configure the "square".
            Label newLabel = new Label();
            newLabel.Size = new Size(40, 40);
            newLabel.BorderStyle = BorderStyle.FixedSingle;

            // To determine where to place the label, you need to convert the 
            // current screen-based mouse coordinates into relative form coordinates.
            newLabel.Location = this.PointToClient(Control.MousePosition);

            // Attach a context menu to the label.
            newLabel.ContextMenu = mnuLabel;

            // Connect the label to all its event handlers.
            newLabel.MouseDown += new MouseEventHandler(lbl_MouseDown);
            newLabel.MouseMove += new MouseEventHandler(lbl_MouseMove);
            newLabel.MouseUp += new MouseEventHandler(lbl_MouseUp);

            // Add the label to the form.
            this.Controls.Add(newLabel);

        }


        // Keep track of when fake drag or resize mode is enabled.
        private bool isDragging = false;
        private bool isResizing = false;

        // Store the location where the user clicked on the control.
        private int clickOffsetX, clickOffsetY;

        private void lbl_MouseDown(object sender,
            System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active label.
            Control currentCtrl;
            currentCtrl = (Control)sender;

            if (e.Button == MouseButtons.Right)
            {
                // Show the context menu.
                currentCtrl.ContextMenu.Show(currentCtrl, new Point(e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            {
                clickOffsetX = e.X;
                clickOffsetY = e.Y;

                if ((e.X + 5) > currentCtrl.Width &amp;&amp; (e.Y + 5) > currentCtrl.Height)
                {
                    // The mouse pointer is in the bottom right corner,
                    // so resizing mode is appropriate.
                    isResizing = true;
                }
                else
                {
                    // The mouse is somewhere else, so dragging mode is
                    // appropriate.
                    isDragging = true;
                }
            }
        }

        private void lbl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active label.
            Control currentCtrl;
            currentCtrl = (Control)sender;

            if (isDragging)
            {
                // Move the control.
                currentCtrl.Left += e.X - clickOffsetX;
                currentCtrl.Top += e.Y  - clickOffsetY;
            }
            else if (isResizing)
            {
                // Resize the control.
                currentCtrl.Width = e.X;
                currentCtrl.Height = e.Y;
            }
            else
            {
                // Change the pointer if the mouse is in the bottom corner.
                if ((e.X + 5) > currentCtrl.Width &amp;&amp; (e.Y + 5) > currentCtrl.Height)
                {
                    currentCtrl.Cursor = Cursors.SizeNWSE;
                }
                else
                {
                    currentCtrl.Cursor = Cursors.Arrow;
                }
            }
        }
        private void lbl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDragging = false;
            isResizing = false;
        }

        private void mnuColorChange_Click(object sender, System.EventArgs e)
        {
            // Show color dialog.
            ColorDialog dlgColor = new ColorDialog();
            dlgColor.ShowDialog();

            // Change label background.
            mnuLabel.SourceControl.BackColor = dlgColor.Color;

        }

        private void DrawingSquares_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.ContextMenu.Show(this, new Point(e.X, e.Y));
            }
        
        }

    }
}


           
          


Drawing Shapes

image_pdfimage_print


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System.Drawing;
using System.Drawing.Drawing2D;

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

    /// <summary>
    /// Summary description for DrawingShapes.
    /// </summary>
    public class DrawingShapes : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ContextMenu mnuLabel;
        internal System.Windows.Forms.MenuItem mnuColorChange;
        internal System.Windows.Forms.ContextMenu mnuForm;
        internal System.Windows.Forms.MenuItem mnuRectangle;
        internal System.Windows.Forms.MenuItem mnuEllipse;
        internal System.Windows.Forms.MenuItem mnuTriangle;
        private System.Windows.Forms.MenuItem mnuRemoveShape;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DrawingShapes()
        {
            //
            // 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()
        {
            this.mnuLabel = new System.Windows.Forms.ContextMenu();
            this.mnuColorChange = new System.Windows.Forms.MenuItem();
            this.mnuForm = new System.Windows.Forms.ContextMenu();
            this.mnuRectangle = new System.Windows.Forms.MenuItem();
            this.mnuEllipse = new System.Windows.Forms.MenuItem();
            this.mnuTriangle = new System.Windows.Forms.MenuItem();
            this.mnuRemoveShape = new System.Windows.Forms.MenuItem();
            // 
            // mnuLabel
            // 
            this.mnuLabel.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.mnuColorChange,
                                                                                     this.mnuRemoveShape});
            // 
            // mnuColorChange
            // 
            this.mnuColorChange.Index = 0;
            this.mnuColorChange.Text = "Change Color";
            this.mnuColorChange.Click += new System.EventHandler(this.mnuColorChange_Click);
            // 
            // mnuForm
            // 
            this.mnuForm.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                    this.mnuRectangle,
                                                                                    this.mnuEllipse,
                                                                                    this.mnuTriangle});
            // 
            // mnuRectangle
            // 
            this.mnuRectangle.Index = 0;
            this.mnuRectangle.Text = "Create New Rectangle";
            this.mnuRectangle.Click += new System.EventHandler(this.mnuNewShape_Click);
            // 
            // mnuEllipse
            // 
            this.mnuEllipse.Index = 1;
            this.mnuEllipse.Text = "Create New Ellipse";
            this.mnuEllipse.Click += new System.EventHandler(this.mnuNewShape_Click);
            // 
            // mnuTriangle
            // 
            this.mnuTriangle.Index = 2;
            this.mnuTriangle.Text = "Create New Triangle";
            this.mnuTriangle.Click += new System.EventHandler(this.mnuNewShape_Click);
            // 
            // mnuRemoveShape
            // 
            this.mnuRemoveShape.Index = 1;
            this.mnuRemoveShape.Text = "Remove Shape";
            this.mnuRemoveShape.Click += new System.EventHandler(this.mnuRemoveShape_Click);
            // 
            // DrawingShapes
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(628, 426);
            this.ContextMenu = this.mnuForm;
            this.Name = "DrawingShapes";
            this.Text = "DrawingShapes";
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawingShapes_MouseDown);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new DrawingShapes());
        }


        // Keep track of when fake drag or resize mode is enabled.
        private bool isDragging = false;
        private bool isResizing = false;

        // Store the location where the user clicked on the control.
        private int clickOffsetX, clickOffsetY;

        private void lbl_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active label.
            Control currentCtrl;
            currentCtrl = (Control)sender;

            if (e.Button == MouseButtons.Right)
            {
                // Show the context menu.
                currentCtrl.ContextMenu.Show(currentCtrl, new Point(e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            {
                clickOffsetX = e.X;
                clickOffsetY = e.Y;

                if ((e.X + 5) > currentCtrl.Width || (e.Y + 5) > currentCtrl.Height)
                {
                    // The mouse pointer is in the bottom right corner,
                    // so resizing mode is appropriate.
                    isResizing = true;
                }
                else
                {
                    // The mouse is somewhere else, so dragging mode is
                    // appropriate.
                    isDragging = true;
                }
            }
        }

        private void lbl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Retrieve a reference to the active shape.
            Control currentCtrl;
            currentCtrl = (Control)sender;

            if (isDragging)
            {
                // Move the control.
                currentCtrl.Left = e.X + currentCtrl.Left - clickOffsetX;
                currentCtrl.Top = e.Y + currentCtrl.Top - clickOffsetY;
            }
            else if (isResizing)
            {
                // Resize the control, according to the resize mode.
                if (currentCtrl.Cursor == Cursors.SizeNWSE)
                {
                    currentCtrl.Width = e.X;
                    currentCtrl.Height = e.Y;
                }
                else if (currentCtrl.Cursor == Cursors.SizeNS)
                {
                    currentCtrl.Height = e.Y;
                }
                else if (currentCtrl.Cursor == Cursors.SizeWE)
                {
                    currentCtrl.Width = e.X;
                }
            }
            else
            {
                // Change the cursor if the mouse pointer is on one of the edges
                // of the control.
                if (((e.X + 5) > currentCtrl.Width) &amp;&amp; 
                    ((e.Y + 5) > currentCtrl.Height))
                {
                    currentCtrl.Cursor = Cursors.SizeNWSE;
                }
                else if ((e.X + 5) > currentCtrl.Width)
                {
                    currentCtrl.Cursor = Cursors.SizeWE;
                }
                else if ((e.Y + 5) > currentCtrl.Height)
                {
                    currentCtrl.Cursor = Cursors.SizeNS;
                }
                else
                {
                    currentCtrl.Cursor = Cursors.Arrow;
                }
            }

        }
        private void lbl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            isDragging = false;
            isResizing = false;
        }

        private void mnuColorChange_Click(object sender, System.EventArgs e)
        {
            // Show color dialog.
            ColorDialog dlgColor = new ColorDialog();
            dlgColor.ShowDialog();

            // Change label background.
            mnuLabel.SourceControl.BackColor = dlgColor.Color;

        }

        private void DrawingShapes_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.ContextMenu.Show(this, new Point(e.X, e.Y));
            }
        
        }

        private void mnuNewShape_Click(object sender, System.EventArgs e)
        {
            // Create and configure the shape with some defaults.
            Shape newShape = new Shape();
            newShape.Size = new Size(40, 40);
            newShape.ForeColor = Color.Coral;

            // Configure the appropriate shape depending on the menu option selected.
            if (sender == mnuRectangle)
            {
                newShape.Type = Shape.ShapeType.Rectangle;
            }
            else if (sender == mnuEllipse)
            {
                newShape.Type = Shape.ShapeType.Ellipse;
            }
            else if (sender == mnuTriangle)
            {
                newShape.Type = Shape.ShapeType.Triangle;
            }

            // To determine where to place the shape, you need to convert the 
            // current screen-based mouse coordinates into relative form coordinates.
            newShape.Location = this.PointToClient(Control.MousePosition);

            // Attach a context menu to the shape.
            newShape.ContextMenu = mnuLabel;

            // Connect the shape to all its event handlers.
            newShape.MouseDown += new MouseEventHandler(lbl_MouseDown);
            newShape.MouseMove += new MouseEventHandler(lbl_MouseMove);
            newShape.MouseUp += new MouseEventHandler(lbl_MouseUp);

            // Add the shape to the form.
            this.Controls.Add(newShape);
        }

        private void mnuRemoveShape_Click(object sender, System.EventArgs e)
        {
             Shape ctrlShape = (Shape)mnuLabel.SourceControl;
            this.Controls.Remove(ctrlShape);
        }
    }
    public class Shape : System.Windows.Forms.UserControl
    {
        // The types of shapes supported by this control.
        public enum ShapeType
        {
            Rectangle,
            Ellipse,
            Triangle
        }

        private ShapeType shape = ShapeType.Rectangle;
        private GraphicsPath path = null;

        public ShapeType Type
        {
            get
            {
                return shape;
            }
            set
            {
                shape = value;
                RefreshPath();
                this.Invalidate();
            }
        }

        // Create the corresponding GraphicsPath for the shape, and apply
        // it to the control by setting the Region property.
        private void RefreshPath()
        {
            path = new GraphicsPath();
            switch (shape)
            {
                case ShapeType.Rectangle:
                    path.AddRectangle(this.ClientRectangle);
                    break;
                case ShapeType.Ellipse:
                    path.AddEllipse(this.ClientRectangle);
                    break;
                case ShapeType.Triangle:
                    Point pt1 = new Point(this.Width / 2, 0);
                    Point pt2 = new Point(0, this.Height);
                    Point pt3 = new Point(this.Width, this.Height);
                    path.AddPolygon(new Point[] {pt1, pt2, pt3});
                    break;
            }
            this.Region = new Region(path);
        }

        protected override void OnResize(System.EventArgs e)
        {
            base.OnResize(e);
            RefreshPath();
            this.Invalidate();
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            if (path != null)
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.FillPath(new SolidBrush(this.BackColor), path);
                e.Graphics.DrawPath(new Pen(this.ForeColor, 4), path);
            }
        }

    }

           
          


Using the mouse to draw on a form

image_pdfimage_print


   


   using System;
   using System.Drawing;
   using System.ComponentModel;
   using System.Windows.Forms;

   public class Painter : System.Windows.Forms.Form
   {
      bool shouldPaint = false;

      public Painter()
      {
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(400, 400);

         this.MouseDown +=new System.Windows.Forms.MouseEventHandler(this.Painter_MouseDown );
         this.MouseUp +=new System.Windows.Forms.MouseEventHandler(this.Painter_MouseUp );
         this.MouseMove +=new System.Windows.Forms.MouseEventHandler(this.Painter_MouseMove );
      }

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

      private void Painter_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e )
      {
         shouldPaint = true;
      }

      private void Painter_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e )
      {
         shouldPaint = false;
      }

      protected void Painter_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e )
      {
         if ( shouldPaint )
         {
            Graphics graphics = CreateGraphics();
            graphics.FillEllipse(new SolidBrush( Color.Black ),e.X, e.Y, 10, 10 );
         }
      }
   }

           
          


Drag and draw

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
   bool shouldPaint = false;
   public Form1() {
       InitializeComponent();
   }

   private void PainterForm_MouseDown( object sender, MouseEventArgs e )
   {
      shouldPaint = true;
   }

   private void PainterForm_MouseUp( object sender, MouseEventArgs e )
   {
      shouldPaint = false;
   }

   private void PainterForm_MouseMove( object sender, MouseEventArgs e )
   {
      if ( shouldPaint )
      {
         Graphics graphics = CreateGraphics();
         graphics.FillEllipse(new SolidBrush( Color.BlueViolet ), e.X, e.Y, 4, 4 );
         graphics.Dispose();
      }
   }
  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(184, 180);
     this.Name = "PainterForm";
     this.Text = "Painter";
     this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PainterForm_MouseDown);
     this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PainterForm_MouseUp);
     this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PainterForm_MouseMove);
     this.ResumeLayout(false);

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

}


           
          


Matrix Draw

image_pdfimage_print


   

/*
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.ComponentModel;
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.ComponentModel.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);
    }
    }
}


           
          


Text direction (Matrix Rotate)

image_pdfimage_print
   
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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();
    }
}

    


new Matrix(0.707f, 0.707f, -0.707f, 0.707f, 0, 0)

image_pdfimage_print
   
 


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));
     }
}