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

クリッピングとは、画像の一部を切り抜くことです。ビットマップでは、Clipper クラスで画像を切り取ることができます。画像全体ではなく、ソース画像をクリップして小さなフラグメントを読み込むため、Clipper クラスを使用してクリッパー変換を渡すことができます。

以下のアニメーションは画像のクリッピングを示しています。

以下のコードは、ボタンのクリックイベントによる画像の切り抜きを実装しています。この例では、「 クイックスタート」で作成したサンプルを使用します。

Private Async Function UpdateImageSource() As Task
    sb = btmp.ToSoftwareBitmap()
    sbs = New SoftwareBitmapSource()
    Await sbs.SetBitmapAsync(sb)
    img.Source = sbs

    img.Width = btmp.PixelWidth
    img.Height = btmp.PixelHeight
End Function

Private Async Function ApplyTransform(t As BaseTransform) As Task
    Dim bm = btmp.Transform(t)
    btmp.Dispose()
    btmp = bm

    Await UpdateImageSource()
End Function

Private Async Sub btnCrop_Click(sender As Object, e As RoutedEventArgs)
    Dim cropRect As New ImageRect(150, 100, 300, 250)
    Await ApplyTransform(New Clipper() With {.ImageRect = cropRect})
End Sub
private async Task UpdateImageSource()
{
    sb = btmp.ToSoftwareBitmap();
    sbs = new SoftwareBitmapSource();
    await sbs.SetBitmapAsync(sb);
    img.Source = sbs;
    
    img.Width = btmp.PixelWidth;
    img.Height = btmp.PixelHeight;
}
        
private async Task ApplyTransform(BaseTransform t)
{
    var bm = btmp.Transform(t);
    btmp.Dispose();
    btmp = bm;

    await UpdateImageSource();
}

private async void btnClip_Click(object sender, RoutedEventArgs e)
{
    Rect select;
    var cropRect = ((RectD)select).Round();
    await ApplyTransform(new Clipper { ImageRect = new ImageRect(150, 100, 300, 250) });
}
関連トピック