Bitmap for UWP
画像の拡大/縮小
機能 > 変換の適用 > 画像の拡大/縮小

スケーリングとは、画像のピクセル数を変更することによって、画像のサイズ変更(サイズの拡大/縮小)を行います。Bitmap には、画像を拡大/縮小するための スケーラ クラスが用意されています。Scaler クラスでは、画像を拡大/縮小するために次の3つのプロパティが必要です。

以下のアニメーションは画像の拡大/縮小を示しています。

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

Private Async Function UpdateImageSource() As Task
    Dim sb As SoftwareBitmap = 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 btnScale_Click(sender As Object, e As RoutedEventArgs)
    Dim px As Integer = btmp.PixelWidth * 1.6F + 0.5F
    Dim py As Integer = btmp.PixelHeight * 1.6F + 0.5F
    Await ApplyTransform(New Scaler(px, py, InterpolationMode.HighQualityCubic))
End Sub


Private Async Sub btnScaleOut_Click(sender As Object, e As RoutedEventArgs)
    Dim px As Integer = btmp.PixelWidth * 0.625F + 0.5F
    Dim py As Integer = btmp.PixelHeight * 0.625F + 0.5F
    If px > 0 AndAlso py > 0 Then
        Await ApplyTransform(New Scaler(px, py, InterpolationMode.HighQualityCubic))
    End If
End Sub
private async Task UpdateImageSource()
{
    SoftwareBitmap 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 btnScale_Click(object sender, RoutedEventArgs e)
{
    int px = (int)(btmp.PixelWidth * 1.6f + 0.5f);
    int py = (int)(btmp.PixelHeight * 1.6f + 0.5f);
    await ApplyTransform(new Scaler(px, py, InterpolationMode.HighQualityCubic));
}

private async void btnScaleOut_Click(object sender, RoutedEventArgs e)
{
    int px = (int)(btmp.PixelWidth * 0.625f + 0.5f);
    int py = (int)(btmp.PixelHeight * 0.625f + 0.5f);
    if (px > 0 && py > 0)
    {
        await ApplyTransform(new Scaler(px, py, InterpolationMode.HighQualityCubic));
    }
}
関連トピック