Bagikan melalui


Cara: Membuat Gambar dari Garis, Kurva, dan Bentuk

Untuk membuat gambar, buat GraphicsPath, lalu panggil metode, seperti AddLine dan AddCurve, untuk menambahkan primitif ke jalur.

Contoh

Contoh kode berikut membuat jalur yang memiliki gambar:

  • Contoh pertama membuat jalur yang memiliki satu gambar. Angka tersebut terdiri dari satu busur. Busur memiliki sudut sapuan –180 derajat, yang berlawanan arah jarum dalam sistem koordinat default.

  • Contoh kedua membuat jalur yang memiliki dua gambar. Angka pertama adalah busur diikuti oleh garis. Gambar kedua adalah garis diikuti oleh kurva diikuti oleh garis. Angka pertama dibiarkan terbuka, dan angka kedua ditutup.

GraphicsPath path = new GraphicsPath();
path.AddArc(175, 50, 50, 50, 0, -180);
e.Graphics.DrawPath(new Pen(Color.FromArgb(128, 255, 0, 0), 4), path);
Dim path As New GraphicsPath()
path.AddArc(175, 50, 50, 50, 0, -180)
e.Graphics.DrawPath(New Pen(Color.FromArgb(128, 255, 0, 0), 4), path)

     // Create an array of points for the curve in the second figure.
     Point[] points = {
new Point(40, 60),
new Point(50, 70),
new Point(30, 90)};

     GraphicsPath path = new GraphicsPath();

     path.StartFigure(); // Start the first figure.
     path.AddArc(175, 50, 50, 50, 0, -180);
     path.AddLine(100, 0, 250, 20);
     // First figure is not closed.

     path.StartFigure(); // Start the second figure.
     path.AddLine(50, 20, 5, 90);
     path.AddCurve(points, 3);
     path.AddLine(50, 150, 150, 180);
     path.CloseFigure(); // Second figure is closed.

     e.Graphics.DrawPath(new Pen(Color.FromArgb(255, 255, 0, 0), 2), path);
' Create an array of points for the curve in the second figure.
Dim points As Point() = { _
   New Point(40, 60), _
   New Point(50, 70), _
   New Point(30, 90)}

Dim path As New GraphicsPath()

path.StartFigure() ' Start the first figure.
path.AddArc(175, 50, 50, 50, 0, -180)
path.AddLine(100, 0, 250, 20)
' First figure is not closed.

path.StartFigure() ' Start the second figure.
path.AddLine(50, 20, 5, 90)
path.AddCurve(points, 3)
path.AddLine(50, 150, 150, 180)
path.CloseFigure() ' Second figure is closed.
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 2), path)

Mengompilasi Kode

Contoh sebelumnya dirancang untuk digunakan dengan Formulir Windows, dan memerlukan PaintEventArgs e, yang merupakan parameter penanganan Paint aktivitas.

Lihat juga