Features / Applying Transformations / Clipping an Image
In This Topic
Clipping an Image
In This Topic

In two-dimensional images, clipping is an essential requirement as it provides selective rendering of pixels within the boundaries of the selected frame area. Bitmap lets you clip the source image and load a part of the whole image using Clipper transformation.

The following image shows the clipping feature.

Apply clipping using ComponentOne Bitmap for WinForms

Complete the following steps to clip an image using Bitmap in code.

  1. Add the following import statement.
    Imports System.Drawing.Drawing2D
    
  2. Initialize a rectangle and a point as global variables in Form1 class.
    'Initialize a rectangle and a point
    Dim selection As New RectF(1.0F, 1.0F)
    Dim start As Point2L
    
  3. Add the following code to apply clipper transformation.
    'Transform method to apply transformation
    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
    
    'Event to apply clipper transformation on button click
    Private Sub Btn_Clip_Click(sender As Object, e As EventArgs) _
        Handles Btn_Clip.Click
        Dim rect = New RectF(selection.X * bitmap.PixelWidth,
                             selection.Y * bitmap.PixelHeight,
                             selection.Width * bitmap.PixelWidth,
                             selection.Height * bitmap.PixelHeight)
        ApplyTransform(New Clipper(New ImageRect(rect.Round())))
    End Sub
    
  4. Add the following code to select a a portion of the image to be clipped.
    'Events to select a portion of image using mouse 
    Private Sub pictureBox1_MouseClick(sender As Object, e As MouseEventArgs) _
        Handles pictureBox1.MouseClick
        If (e.Button And MouseButtons.Left) <> 0 Then
            Dim dcs = SystemInformation.DoubleClickSize
            If Math.Abs(e.X - start.X) _
                < dcs.Width AndAlso Math.Abs(e.Y - start.Y) < dcs.Height Then
                selection = New RectF(1.0F, 1.0F)
                pictureBox1.Invalidate()
            End If
        End If
    End Sub
    
    Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs) _
        Handles pictureBox1.MouseDown
        If (e.Button And MouseButtons.Left) <> 0 Then
            start = New Point2L(e.X, e.Y)
        End If
    End Sub
    
    Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs) _
        Handles pictureBox1.MouseMove
        If (e.Button And MouseButtons.Left) <> 0 Then
            Dim w As Integer = pictureBox1.Width
            Dim h As Integer = pictureBox1.Height
            Dim x As Integer = Math.Max(0, Math.Min(e.X, w))
            Dim y As Integer = Math.Max(0, Math.Min(e.Y, h))
    
            selection = New RectF(CSng(Math.Min(start.X, x)) / w,
                                  CSng(Math.Min(start.Y, y)) / h,
                                  CSng(Math.Abs(x - start.X)) / w,
                                  CSng(Math.Abs(y - start.Y)) / h)
    
            pictureBox1.Invalidate()
        End If
    End Sub
    
    Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs) _
        Handles pictureBox1.Paint
        Dim w As Integer = pictureBox1.Width
        Dim h As Integer = pictureBox1.Height
        Dim path = New GraphicsPath(FillMode.Alternate)
        path.AddRectangle(New RectangleF(0, 0, w, h))
        path.AddRectangle(New RectangleF(selection.X * w,
                                         selection.Y * h,
                                         selection.Width * w,
                                         selection.Height * h))
    
        Dim brush = New SolidBrush(Color.FromArgb(&H66FFFFFF))
        e.Graphics.FillPath(brush, path)
        brush.Dispose()
    
        path.Dispose()
    End Sub
    
  5. Press F5 to run the application and click 'Load Image' button to load an image.
  6. Select a portion of the image through mouse and click 'Clip Image' button to crop the selected portion.
See Also