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
See Also