0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#で塗りつぶし描画アプリを作る

Posted at

以前、ピクチャボックス内をマーカーでベタ塗りするアプリを作った。
それを改良して、ボタンを押した時だけピクチャボックスが塗りつぶされるアプリを作成する。

追加仕様
・ピクチャボックス内をマーカーが移動する
・保存ボタンを押すと、マーカーが表示される位置が塗りつぶされる。
・塗りつぶされた部分は消えない。

C#&アプリ初心者の練習です。

アプリの作成

環境:Windows+VisualStudio(Winforms)

drawpolygon.cs
    public partial class Form1 : Form
    {
        int SaveTrigger = 0;
        List<PointF[]> list = new List<PointF[]>();

        public Form1()
        {
            InitializeComponent();           
        }

        private void PastePicture(object sender, PaintEventArgs e)
        {
            int mouseY = Decimal.ToInt32(numericUpDown1.Value);

            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(88, 0, 0, 255));
            Brush brush = new SolidBrush(Color.Gray);

            PointF point1 = new PointF(0, mouseY + 100);
            PointF point2 = new PointF(pictureBox1.Width, mouseY + 100);
            PointF point3 = new PointF(pictureBox1.Width, mouseY - 100);
            PointF point4 = new PointF(0, mouseY - 100);

            PointF[] curvePoints =
            {
                 point1,
                 point2,
                 point3,
                 point4
            };

            // 保存が押された時の座標を保存
            if(SaveTrigger == 1)
            { 
                list.Add(curvePoints);
                SaveTrigger = 0;
            }

            for (int i = 0; i < list.Count; i++)
            {
                e.Graphics.FillPolygon(brush, list[i]);
            }

            // 常にマーカーを描画
            e.Graphics.FillPolygon(semiTransBrush, curvePoints);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            pictureBox1.Invalidate();
        }

        private void save_Click(object sender, EventArgs e)
        {
            pictureBox1.Invalidate();
            SaveTrigger = 1;
        }
    }

Paintイベント内に「マーカーの描画」と「塗りつぶし部分の描画」をそれぞれ作成した。
「マーカーの描画」は、数値入力ボックスの値を座標とする。
「塗りつぶし部分の描画」は、保存ボタンが押された時だけ実行される。
(Paintイベントにパラメータ情報を渡す方法がわからず、保存ボタンが押された時のみ実行するかどうかの判定にSaveTriggerというグローバル変数を使ってしまった。良い方法があれば教えて下さい。)

実行画面
2021-01-09_18h46_52.gif

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?