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

画像の拡大/縮小(スケーリング)は、画像のリサイズ(サイズの増減)に伴う画像処理の重要な要件です。Bitmap では、Scaler クラスの InterpolationMode プロパティを使用して、画像の拡大/縮小を行うことができます。

以下の画像は、拡大/縮小機能を示しています。

以下のコードは、ボタンクリック時の画像の拡大/縮小を示しています。この例では、「クイックスタート」セクションで作成したサンプルを使用します。

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 Button2_Click(sender As Object, e As EventArgs) _
    Handles Button2.Click
    Dim px As Integer = CInt(bitmap.PixelWidth * 1.6F + 0.5F)
    Dim py As Integer = CInt(bitmap.PixelHeight * 1.6F + 0.5F)
    ApplyTransform(New Scaler(px, py, _
                    C1.Win.Bitmap.InterpolationMode.HighQualityCubic))
End Sub

'ボタンをクリックする時に画像を拡大するイベント
Private Sub Button3_Click(sender As Object, e As EventArgs) _
    Handles Button3.Click
    Dim px As Integer = CInt(bitmap.PixelWidth * 0.625F + 0.5F)
    Dim py As Integer = CInt(bitmap.PixelHeight * 0.625F + 0.5F)
    If px > 0 AndAlso py > 0 Then
        ApplyTransform(New Scaler(px, py, _
                        C1.Win.Bitmap.InterpolationMode.HighQualityCubic))
    End If
End Sub
void ApplyTransform(BaseTransform t)
{
    var newBitmap = bitmap.Transform(t);
    bitmap.Dispose();

    bitmap = newBitmap;
    selection = new RectF(1f, 1f);

    UpdateImage();
}
        
//ボタンをクリックする時に画像を縮小するイベント
private void button3_Click(object sender, EventArgs e)
{
    int px = (int)(bitmap.PixelWidth * 1.6f + 0.5f);
    int py = (int)(bitmap.PixelHeight * 1.6f + 0.5f);
    ApplyTransform(new Scaler(px, py, 
        C1.Win.Bitmap.InterpolationMode.HighQualityCubic));
}

//ボタンをクリックする時に画像を拡大するイベント
private void button4_Click(object sender, EventArgs e)
{
    int px = (int)(bitmap.PixelWidth * 0.625f + 0.5f);
    int py = (int)(bitmap.PixelHeight * 0.625f + 0.5f);
    if (px > 0 && py > 0)
    {
        ApplyTransform(new Scaler(px, py, 
            C1.Win.Bitmap.InterpolationMode.HighQualityCubic));
    }
}
関連トピック