【C#】【Form】PictureBox [10] ~ 画像をレイヤー構造で扱う ~

■ サンプル

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

namespace SampleForm
{
  public partial class Form1 : Form
  {
    /// <summary>
    /// オリジナルのビットマップ
    /// </summary>
    private Bitmap originalBitmap = null;
    private Bitmap childBitmap = null;
    private Bitmap grandChildBitmap = null;

    //ピクチャーボックスの生成
    private PictureBox childPictureBox = null;
    private PictureBox grandChildPictureBox = null;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      // 画像ファイルのImageオブジェクトを作成する
      this.originalBitmap = new Bitmap(@"20161215052204.gif");
      this.pictureBox1.Image = this.originalBitmap;

      // 子・孫のインスタンス生成
      this.childPictureBox = new PictureBox();
      this.grandChildPictureBox = new PictureBox();

      // 親コントロールを設定
      this.childPictureBox.Parent = pictureBox1;
      this.grandChildPictureBox.Parent = this.childPictureBox;

      this.childPictureBox.Dock = DockStyle.Fill;
      this.grandChildPictureBox.Dock = DockStyle.Fill;

      // 背景を透過する
      this.pictureBox1.BackColor = Color.Transparent;
      this.childPictureBox.BackColor = Color.Transparent;
      this.grandChildPictureBox.BackColor = Color.Transparent;

      this.grandChildPictureBox.MouseDown += new MouseEventHandler(this.OnMouseDown);
    }

    private void OnMouseDown(object sender, MouseEventArgs e)
    {
      // 子PictureBox
      this.childBitmap = new Bitmap(this.childPictureBox.Width, this.childPictureBox.Height);
      using (var graphics = Graphics.FromImage(this.childBitmap))
      {
        graphics.DrawLine(new Pen(Color.Red, 4), new Point(5, 5), e.Location);
      }
      if (this.childPictureBox.Image != null)
      {
        this.childPictureBox.Image.Dispose();
      }
      this.childPictureBox.Image = this.childBitmap;

      // 孫PictureBox
      this.grandChildBitmap = new Bitmap(this.grandChildPictureBox.Width, this.grandChildPictureBox.Height);
      using (var font = new Font("メイリオ", 10))
      using (var graphics = Graphics.FromImage(this.grandChildBitmap))
      {
        graphics.DrawString("テスト中。", font, Brushes.Blue, e.Location);
      }
      if (this.grandChildPictureBox.Image != null)
      {
        this.grandChildPictureBox.Image.Dispose();
      }
      this.grandChildPictureBox.Image = this.grandChildBitmap;
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
      var checkBox = sender as CheckBox;
      if (checkBox.Checked && this.childBitmap != null)
      {
        this.childPictureBox.Image = this.childBitmap;
      }
      else
      {
        this.childPictureBox.Image = null;
      }
    }

    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
      var checkBox = sender as CheckBox;
      if (checkBox.Checked && this.grandChildBitmap != null)
      {
        this.grandChildPictureBox.Image = this.grandChildBitmap;
      }
      else
      {
        this.grandChildPictureBox.Image = null;
      }
    }
  }
}


関連記事

Windows Form

Windows Form ~ 目次 ~
https://blogs.yahoo.co.jp/dk521123/8054245.html

Windows Form / PictureBox

PictureBox [8] ~ 画像比較スライダーをWindows Formで実装する ~
https://blogs.yahoo.co.jp/dk521123/38051736.html