header
CodePanic! > C#.NET Tips > 今ここ

■直線を描画する

PictureBoxに直線を描画する例です。
gra_line.jpg(17116 byte)

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Graphics gra = pictureBox1.CreateGraphics())
            {
                // 既存のPenを使って黒の直線
                gra.DrawLine(Pens.Black, 0, 0, pictureBox1.Width, 0);

                // 青色、太さ3のペンで直線
                Pen pen = new Pen(Color.Blue, 3);
                gra.DrawLine(pen, 0, 10, pictureBox1.Width, 10);

                // ペンを破線にして直線
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                gra.DrawLine(pen, 0, 20, pictureBox1.Width, 20);
            }
        }
    }
}





Copyright © 2008.07 - shougo suzaki