Bitmap for WinForms
画像のクリッピング
機能 > 変換の適用 > 画像のクリッピング

2次元画像において、選択されたフレーム領域の境界内のピクセルの選択的レンダリングを提供するにあたっては、クリッピングが必須の要件となります。Bitmap は、クリッパー変換を使用してソース画像をクリップし、画像全体の一部を読み込むことができます。

以下の画像は、クリッピング機能を示しています。

コード内で Bitmap を使用してイメージをクリップするには、次の手順を実行します。

  1. 次の import ステートメントを追加します。
    Imports System.Drawing.Drawing2D
    
    using System.Drawing.Drawing2D;
    
  2. Form1 クラスにて、矩形と点をグローバル変数として初期化します。
    '長方形と点を初期化します
    Dim selection As New RectF(1.0F, 1.0F)
    Dim start As Point2L
    
    //長方形と点を初期化します
    RectF selection = new RectF(1f, 1f);
    Point2L start;
    
  3. クリッパー変換を適用するために、以下のコードを追加します。
    '変換を適用するためのメソッド
    Private Sub ApplyTransform(t As BaseTransform)
        Dim newBitmap = bitmap.Transform(t)
        bitmap.Dispose()
        bitmap = newBitmap
        selection = New RectF(1.0F, 1.0F)
        UpdateImage()
    End Sub
    
    'ボタンクリック時にクリッパー変換を適用するイベント
    Private Sub Btn_Clip_Click(sender As Object, e As EventArgs) _
        Handles Btn_Clip.Click
        Dim rect = New RectF(selection.X * bitmap.PixelWidth,
                             selection.Y * bitmap.PixelHeight,
                             selection.Width * bitmap.PixelWidth,
                             selection.Height * bitmap.PixelHeight)
        ApplyTransform(New Clipper(New ImageRect(rect.Round())))
    End Sub
    
    //変換を適用するためのメソッド
    void ApplyTransform(BaseTransform t)
    {
        var newBitmap = bitmap.Transform(t);
        bitmap.Dispose();
    
        bitmap = newBitmap;
        selection = new RectF(1f, 1f);
    
        UpdateImage();
    }
    
    //ボタンクリック時にクリッパー変換を適用するイベント
    private void button2_Click(object sender, EventArgs e)
    {
        var rect = new RectF(selection.X * bitmap.PixelWidth,
           selection.Y * bitmap.PixelHeight,
           selection.Width * bitmap.PixelWidth,
           selection.Height * bitmap.PixelHeight);
    
        ApplyTransform(new Clipper(new ImageRect(rect.Round())));
    }
    
  4. クリップされる画像の一部分を選択するために、以下のコードを追加します。
    'マウスを使用してピクチャボックスから画像の一部を選択するイベント 
    Private Sub pictureBox1_MouseClick(sender As Object, e As MouseEventArgs) _
        Handles pictureBox1.MouseClick
        If (e.Button And MouseButtons.Left) <> 0 Then
            Dim dcs = SystemInformation.DoubleClickSize
            If Math.Abs(e.X - start.X) _
                < dcs.Width AndAlso Math.Abs(e.Y - start.Y) < dcs.Height Then
                selection = New RectF(1.0F, 1.0F)
                pictureBox1.Invalidate()
            End If
        End If
    End Sub
    
    Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs) _
        Handles pictureBox1.MouseDown
        If (e.Button And MouseButtons.Left) <> 0 Then
            start = New Point2L(e.X, e.Y)
        End If
    End Sub
    
    Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs) _
        Handles pictureBox1.MouseMove
        If (e.Button And MouseButtons.Left) <> 0 Then
            Dim w As Integer = pictureBox1.Width
            Dim h As Integer = pictureBox1.Height
            Dim x As Integer = Math.Max(0, Math.Min(e.X, w))
            Dim y As Integer = Math.Max(0, Math.Min(e.Y, h))
    
            selection = New RectF(CSng(Math.Min(start.X, x)) / w,
                                  CSng(Math.Min(start.Y, y)) / h,
                                  CSng(Math.Abs(x - start.X)) / w,
                                  CSng(Math.Abs(y - start.Y)) / h)
    
            pictureBox1.Invalidate()
        End If
    End Sub
    
    Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs) _
        Handles pictureBox1.Paint
        Dim w As Integer = pictureBox1.Width
        Dim h As Integer = pictureBox1.Height
        Dim path = New GraphicsPath(FillMode.Alternate)
        path.AddRectangle(New RectangleF(0, 0, w, h))
        path.AddRectangle(New RectangleF(selection.X * w,
                                         selection.Y * h,
                                         selection.Width * w,
                                         selection.Height * h))
    
        Dim brush = New SolidBrush(Color.FromArgb(&H66FFFFFF))
        e.Graphics.FillPath(brush, path)
        brush.Dispose()
    
        path.Dispose()
    End Sub
    
    //マウスを使用してピクチャボックスから画像の一部を選択するイベント
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) != 0)
        {
            start = new Point2L(e.X, e.Y);
        }
    }
    
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) != 0)
        {
            var dcs = SystemInformation.DoubleClickSize;
            if (Math.Abs(e.X - start.X) 
                < dcs.Width && Math.Abs(e.Y - start.Y) < dcs.Height)
            {
                selection = new RectF(1f, 1f);
                pictureBox1.Invalidate();
            }
        }
    }
    
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) != 0)
        {
            int w = pictureBox1.Width;
            int h = pictureBox1.Height;
            int x = Math.Max(0, Math.Min(e.X, w));
            int y = Math.Max(0, Math.Min(e.Y, h));
    
            selection = new RectF(
                (float)Math.Min(start.X, x) / w,
                (float)Math.Min(start.Y, y) / h,
                (float)Math.Abs(x - start.X) / w,
                (float)Math.Abs(y - start.Y) / h);
    
            pictureBox1.Invalidate();
        }
    }
    
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        int w = pictureBox1.Width;
        int h = pictureBox1.Height;
        var path = new GraphicsPath(FillMode.Alternate);
        path.AddRectangle(new RectangleF(0, 0, w, h));
        path.AddRectangle(new RectangleF(selection.X * w, selection.Y * h, 
            selection.Width * w, selection.Height * h));
    
        var brush = new SolidBrush(Color.FromArgb(0x66FFFFFF));
        e.Graphics.FillPath(brush, path);
        brush.Dispose();
    
        path.Dispose();
    }
    
  5. F5 キーを押してアプリケーションを実行し、「画像の読み込み」ボタンをクリックして画像を読み込みます。
  6. 画像の一部をマウスで選択し、「画像のクリッピング」ボタンをクリックすると、選択された部分が切り取られます。
関連トピック