Drag and draw


   


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


   

/*
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)

   
 
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)

   
 


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

    


Enumerate LinearGradientMode

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

public class MainForm : Form {
public MainForm() {
CenterToScreen();
}

protected void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
Rectangle r = new Rectangle(10, 10, 100, 100);
LinearGradientBrush theBrush = null;
int yOffSet = 10;
Array obj = Enum.GetValues(typeof(LinearGradientMode));
for (int x = 0; x < obj.Length; x++) { LinearGradientMode temp = (LinearGradientMode)obj.GetValue(x); theBrush = new LinearGradientBrush(r, Color.GreenYellow, Color.Blue, temp); g.DrawString(temp.ToString(), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 0, yOffSet); g.FillRectangle(theBrush, 150, yOffSet, 200, 50); yOffSet += 80; } } } [/csharp]

LinearGradientMode.Horizontal, Vertical, BackwardDiagonal, ForwardDiagonal

   
 
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 = e.Graphics;
        Font f = new Font(new FontFamily("Times New Roman"), 10);
        Brush fb = new SolidBrush(Color.Black);
        LinearGradientBrush lGB;  
        Color cR = Color.Red, cW = Color.White;
        int w = 100, h = 70;

        g.DrawString("Horizontal", f, fb, 10, 5);
        Rectangle rec = new Rectangle(10, 20, w, h);
        LinearGradientMode lGM = LinearGradientMode.Horizontal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        g.DrawString("Vertical", f, fb, w + 20, 5);
        rec.Offset(w + 10, 0);
        lGM = LinearGradientMode.Vertical;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        g.DrawString("ForwardDiagonal", f, fb, 10, h + 25);
        rec.Offset(-w - 10, h + 20);
        lGM = LinearGradientMode.ForwardDiagonal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        g.DrawString("BackwardDiagonal", f, fb, w + 20, h + 25);
        rec.Offset(w + 10, 0);
        lGM = LinearGradientMode.BackwardDiagonal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);

        fb.Dispose();
        g.Dispose();
    }
}

    


Rectangle Linear-Gradient Brush

   
 


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class RectangleLinearGradientBrush: Form
{
     MenuItem miChecked;
   
     public static void Main()
     {
          Application.Run(new RectangleLinearGradientBrush());
     } 
     public RectangleLinearGradientBrush()
     {
          ResizeRedraw = true; 
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&amp;Gradient-Mode");
   
          foreach (LinearGradientMode gm in 
                              Enum.GetValues(typeof(LinearGradientMode)))
          {
               MenuItem mi = new MenuItem();
               mi.Text     = gm.ToString(); 
               mi.Click   += new EventHandler(MenuGradientModeOnClick);
               Menu.MenuItems[0].MenuItems.Add(mi);
          }
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuGradientModeOnClick(object obj, EventArgs ea)
     {
          miChecked.Checked = false;
          miChecked = (MenuItem) obj;
          miChecked.Checked = true;
          Invalidate();
     }
     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)
     {
          Rectangle rectBrush = 
                         new Rectangle(cx / 4, cy / 4, cx / 2, cy / 2);
   
          LinearGradientBrush lgbrush = 
               new LinearGradientBrush(
                         rectBrush, Color.White, Color.Black,
                         (LinearGradientMode) miChecked.Index);
   
         grfx.FillRectangle(lgbrush, 0, 0, cx, cy);
         grfx.DrawRectangle(Pens.Black, rectBrush);
     }
}