Csharp/CSharp Tutorial/2D/Graphics

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create Graphics from Form object

using System;
using System.Drawing;
using System.Windows.Forms;
   
class Scribble: Form
{
     bool  bTracking;
     Point ptLast;
   
     public static void Main()
     {
          Application.Run(new Scribble());
     }
     public Scribble()
     {
          Text = "Scribble";
     }
     protected override void OnMouseDown(MouseEventArgs mea)
     {
          if (mea.Button != MouseButtons.Left)
               return;
   
          ptLast = new Point(mea.X, mea.Y);
          bTracking = true;
     }
     protected override void OnMouseMove(MouseEventArgs mea)
     {
          if (!bTracking)
               return;
   
          Point ptNew = new Point(mea.X, mea.Y);
          
          Graphics grfx = CreateGraphics();
          grfx.DrawLine(new Pen(ForeColor), ptLast, ptNew);
          grfx.Dispose();
   
          ptLast = ptNew;
     }
     protected override void OnMouseUp(MouseEventArgs mea)
     {
          bTracking = false;
     }
}

Double Buffering Example

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

  class Form1 : Form
  {
    public Form1()
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics displayGraphics = e.Graphics;
      Random r = new Random();
      Image i = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
      Graphics g = Graphics.FromImage(i);
      g.FillRectangle(Brushes.White, ClientRectangle);
      for (int x = 0; x < ClientRectangle.Width; x++)
      {
        for (int y = 0; y < ClientRectangle.Height; y += 10)
        {
          Color c = Color.FromArgb(r.Next(255), r.Next(255),r.Next(255));
          Pen p = new Pen(c, 1);
          g.DrawLine(p, new Point(0, 0), new Point(x, y));
          g.DrawLine(p, new Point(10, 10), new Point(x, y));
          g.DrawLine(p, new Point(20, 10), new Point(x, y));
          g.DrawLine(p, new Point(30, 10), new Point(x, y));
          g.DrawLine(p, new Point(40, 10), new Point(x, y));
          g.DrawLine(p, new Point(50, 10), new Point(x, y));
          g.DrawLine(p, new Point(60, 10), new Point(x, y));
          g.DrawLine(p, new Point(70, 10), new Point(x, y));
          g.DrawLine(p, new Point(80, 10), new Point(x, y));
          g.DrawLine(p, new Point(90, 10), new Point(x, y));
          p.Dispose();
        }
      }
      displayGraphics.DrawImage(i, ClientRectangle);
      i.Dispose();
    }
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
  }

Draw along with the Tab

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Font f = new Font("Times New Roman", 12);
    Font bf = new Font(f, FontStyle.Bold);
    StringFormat sf = new StringFormat();
    float[] ts = { 10.0f, 70.0f, 100.0f, 90.0f };
    sf.SetTabStops(0.0f, ts);
    string s1 = "\tA\tAA\tAAA\tAAAA";
    string s2 = "\tAAAA\tAAA\tAA\tA";
    string s3 = "\tAAAAAAAA\tAAAAAAA\tAAAAAA\tAAAAA\n\tAAAA\tAAA\tAA\tAA";
    g.DrawString(s1, bf, Brushes.Black, 20, 20, sf);
    g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf);
    g.DrawString(s3, f, Brushes.Blue, 20,
                      20 + bf.Height + f.Height, sf);
    f.Dispose();
    bf.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

Draw String

using System;
using System.Drawing;
using System.Windows.Forms;
class PaintHello
{
     public static void Main()
     {
          Form form      = new Form();
          form.Text      = "Paint Hello";
          form.BackColor = Color.White;
          form.Paint    += new PaintEventHandler(MyPaintHandler);
   
          Application.Run(form);
     }
     static void MyPaintHandler(object objSender, PaintEventArgs pea)
     {
          Form     form = (Form)objSender;
          Graphics graphics = pea.Graphics;
   
          graphics.DrawString("Hello, world!", form.Font, Brushes.Black, 0, 0);
     }
}

Graphics.DrawIcon

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 {
    private void Form1_Paint(object sender,
        System.Windows.Forms.PaintEventArgs p) {
        Graphics g = p.Graphics;
    }
    private void createManually() {
        Graphics g;
        g = this.CreateGraphics();
    }
    private void createFromFile() {
        Graphics g;
        Bitmap b;
        b = new Bitmap("C:\E.bmp");
        g = Graphics.FromImage(b);
    }
    private void drawLine() {
        Graphics g;
        g = this.CreateGraphics();
        Icon i = new Icon(@"C:\Desktop.ico");
        g.DrawIcon(i, 150, 15);
    }
}

Graphics.DrawImage

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    Bitmap bmp = new Bitmap("rama.jpg");
    Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
    g.DrawImage(bmp, r, r, GraphicsUnit.Pixel);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

Graphics.DrawLine

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
   
class PrintableForm: Form
{
     public static void Main()
     {
          Application.Run(new PrintableForm());
     }
     public PrintableForm()
     {
          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)
     {
          Pen pen = new Pen(clr);
   
          grfx.DrawLine(pen, 0,      0, cx - 1, cy - 1);
          grfx.DrawLine(pen, cx - 1, 0, 0,      cy - 1);
     }
}

Graphics.DrawLines

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
   
class PrintableForm: Form
{
     public static void Main()
     {
          Application.Run(new PrintableForm());
     }
     public PrintableForm()
     {
          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)
     {
          Point[] apt = {new Point(0,      0),
                         new Point(cx - 1, 0),
                         new Point(cx - 1, cy - 1),
                         new Point(0,      cy - 1),
                         new Point(0,      0)};
   
          grfx.DrawLines(new Pen(clr), apt);
     }
}

No Double Buffering Example

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
  class Form1 : Form
  {
    public Form1()
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      Random r = new Random();
      g.FillRectangle(Brushes.White, ClientRectangle);
      for (int x = 0; x < ClientRectangle.Width; x++)
      {
        for (int y = 0; y < ClientRectangle.Height; y += 10)
        {
          Color c = Color.FromArgb(r.Next(255), r.Next(255),r.Next(255));
          using (Pen p = new Pen(c, 1))
          {
            g.DrawLine(p, new Point(0, 0), new Point(x, y));
            g.DrawLine(p, new Point(10, 40), new Point(x, y));
            g.DrawLine(p, new Point(20, 30), new Point(x, y));
            g.DrawLine(p, new Point(30, 20), new Point(x, y));
            g.DrawLine(p, new Point(40, 10), new Point(x, y));
            g.DrawLine(p, new Point(50, 10), new Point(x, y));                                                            
          }
        }
      }
    }
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
  }

Set SmoothingMode

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Pen p = new Pen(Color.Black, 10);
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    g.DrawLine(p, 30, 30, 80, 30);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

TranslateTransform a Graphics

using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Font f = new Font("Times New Roman", 24);
    g.DrawString("Translation", f, Brushes.Black, 0, 0);
    g.TranslateTransform(150, 75);
    g.DrawString("Translation", f, Brushes.Black, 0, 0);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}