6月 12

文字列を縦に描画する

PictureBox内に文字列を縦に描画する例です。
頭に@が付く縦書き用フォントを利用することに注意です。

gra_stringtate

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
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.White;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Graphics g = pictureBox1.CreateGraphics())
            {
                // 縦書き用フォントを利用
                using (Font font = new Font("@MS ゴシック", 24))
                {
                    // 縦方向にフォーマット
                    StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);

                    g.DrawString("てすと", font, Brushes.Black, 10, 10, sf);
                }
            }
        }
    }
}

6月 12

文字列を矩形内で折り返して描画する

PictureBox内に文字列を折り返して描画する例です。

gra_stringrect

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
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.White;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Graphics g = pictureBox1.CreateGraphics())
            {
                using (Font font = new Font("MS ゴシック", 24))
                {
                	// PictureBoxのクライアント領域いっぱいを矩形とします
                    RectangleF rect = new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height);

                    g.DrawString("012345678901234567890123456789", font, Brushes.Black, rect);
                }
            }
        }
    }
}

6月 12

文字列を描画する

PictureBoxに文字列を描画する例です。

gra_string

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
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.White;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Graphics g = pictureBox1.CreateGraphics())
            {
                using (Font font = new Font("MS ゴシック", 24))
                {
                    g.DrawString("てすと", font, Brushes.Black, 0, 0);
                }
            }
        }
    }
}

6月 12

円を描画する

PictureBoxに円を描画する例です。

gra_ellipse

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())
            {
                // 円を描画
                gra.DrawEllipse(Pens.Black, 0, 0, 30, 30);

                // 中が塗りつぶされた円を描画
                gra.FillEllipse(Brushes.Blue, 60, 0, 50, 50);
            }
        }
    }
}

6月 12

四角形を描画する

PictureBoxに四角形を描画する例です。

gra_rect

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())
            {
                // 四角形
                gra.DrawRectangle(Pens.Black, 0, 0, 30, 30);

                // 中が塗りつぶされた四角形
                gra.FillRectangle(Brushes.Blue, 40, 0, 50, 50);
            }
        }
    }
}

6月 12

直線を描画する

PictureBoxに直線を描画する例です。

gra_line

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