0 Replies - 1288 Views - Last Post: 24 February 2016 - 02:08 PM

#1 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1470
  • View blog
  • Posts: 3,725
  • Joined: 01-February 13

Shaped Image Cropping

Posted 24 February 2016 - 02:08 PM

This is a small example of cropping an image using Shapes that you can create and add to a GraphicsPath. I only have it set up with a Rectangle Shape and a Circle Shape in this example but, you can easily create and add any shapes you want to the GraphicsPath to crop images with.

The Images returned from the CropImage function are formatted as 32bppArgb which if you want to save to the hard drive, you should save them in a Png Image Format which allows transparency.

Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

Public Class Form1
    Private MyImage As New Bitmap("C:\TestFolder\MyPicture.jpg")

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        MyImage.Dispose() 'always dispose of any new class scoped Bitmaps when you`re done with them or when the form is closing
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        PictureBox2.SizeMode = PictureBoxSizeMode.Zoom
        PictureBox3.SizeMode = PictureBoxSizeMode.Zoom

        PictureBox1.Image = MyImage

        Dim CropRect As New Rectangle(70, 20, 140, 200) 'a rectangle that specifies the area of the image we want to crop

        PictureBox2.Image = CropImage(MyImage, CropRect, CropShape.Rectangle) 'crop the image using a Rectangle Shape
        PictureBox3.Image = CropImage(MyImage, CropRect, CropShape.Circle)    'crop the image using a Circle Shape
    End Sub

    ''' <summary>An enum of shapes used for the CropShp parameter of the CropImage function.</summary>
    Private Enum CropShape As Integer
        Rectangle = 0
        Circle = 1
    End Enum

    ''' <summary>Crops a bitmap image to the specified size and shape.</summary>
    ''' <param name="Img">The bitmap image to crop.</param>
    ''' <param name="CropRct">Specifies the location and size of the area to crop the image.</param>
    ''' <param name="CropShp">Specifies the shape to crop from the image.</param>
    Private Function CropImage(ByVal Img As Bitmap, ByVal CropRct As Rectangle, ByVal CropShp As CropShape) As Bitmap

        'first, check for any parameters that may have invalid argumets
        If Img Is Nothing Then Throw New NullReferenceException("The Img parameter must not be Nothing.")
        If CropRct.Width <= 0 Or CropRct.Height <= 0 Or CropRct.Right > Img.Width Or CropRct.Bottom > Img.Height Then
            Throw New ArgumentOutOfRangeException("CropRect", "The croping rectangle width and height must be greater than zero but, not greater than the Image width and height.")
        End If

        'create a new 32bpp Argb bitmap to draw the cropped image onto
        Dim WorkingBitmap As New Bitmap(CropRct.Width, CropRct.Height, PixelFormat.Format32bppArgb)

        Using grx As Graphics = Graphics.FromImage(WorkingBitmap) 'create a new Graphics object of the WorkingBitmap
            grx.SmoothingMode = SmoothingMode.AntiAlias 'set the smothing mode to draw the rounded image edges smoother

            Using tb As New TextureBrush(Img) 'use the Img as the texture for the TextureBrush
                tb.TranslateTransform(-CropRct.X, -CropRct.Y) 'offset the X and Y location of the TextureBrush so it starts at the desired X and Y location of the croping rectangle

                Using gpth As New GraphicsPath 'create a new GraphicsPath

                    Select Case CropShp

                        Case CropShape.Rectangle
                            gpth.AddRectangle(New Rectangle(0, 0, CropRct.Width, CropRct.Height))

                        Case CropShape.Circle
                            gpth.AddEllipse(0, 0, CropRct.Width - 1, CropRct.Height - 1) 'add an ellipse to the GraphicsPath that is the size of the croping rectangle

                    End Select

                    grx.FillPath(tb, gpth) 'draw the GraphicsPath (shape) on the WoringBitmap with the TextureBrush

                End Using 'dispose of the new GraphicsPath

            End Using 'dispose of the new TextureBrush

        End Using 'dispose of the new Graphics object of the WorkingBitmap

        Return WorkingBitmap 'return the new cropped bitmap image
    End Function
End Class



Attached Image

This post has been edited by IronRazer: 24 February 2016 - 03:40 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1