ComponentOne Bitmap for UWP
Features / Applying Transformations / Scaling an Image
In This Topic
    Scaling an Image
    In This Topic

    Scaling helps in resizing (reducing and enlarging the size) of an image by changing the number of pixels in the image. Bitmap provides Scaler class for scaling an image. The Scaler class requires three properties to scale an image, which are as follows:

    The GIF given below shows scaling in and out of an image.

    The following code implements scaling in on a button click event and scaling out on another button's click event. The example uses the sample created in Quick Start.

    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));
        }
    }
    
    See Also