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

    Clipping is cropping a portion of an image. Bitmap allows clipping an image with the Clipper class. To clip the source image and load a small fragment instead of the whole image, You can pass the Clipper transformation using the Clipper class.

    The GIF given below shows clipping an image.

    The following code implements clipping of an image on a button's click event. The example uses the sample created in Quick Start.

    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(100, 50, 250, 200) });
    }
    
    See Also