Getting Region From Bitmap VB.net

Hello Friend Here is tutor to get Region From Image OR Bitmap 
 You Can use it on PictureBox .
 

First of All Enter Following Code Outside And Sub and inside class :-

Private Function GetRegion(ByVal bm As Bitmap, ByVal _
   bg_color As Color) As Region
        Dim new_region As New Region
        new_region.MakeEmpty()

        Dim rect As New Rectangle
        Dim in_image As Boolean = False
        Dim X As Integer

        For Y As Integer = 0 To bm.Height - 1
            X = 0
            Do While (X < bm.Width)
                If Not in_image Then
                    If Not bm.GetPixel(X, Y).Equals(bg_color) _
                        Then
                        in_image = True
                        rect.X = X
                        rect.Y = Y
                        rect.Height = 1
                    End If
                ElseIf bm.GetPixel(X, Y).Equals(bg_color) Then
                    in_image = False
                    rect.Width = (X - rect.X)
                    new_region.Union(rect)
                End If
                X = (X + 1)
            Loop

            ' Add the final piece if necessary.
            If in_image Then
                in_image = False
                rect.Width = (bm.Width - rect.X)
                new_region.Union(rect)
            End If
        Next Y

        Return new_region
    End Function

    Function SetRegion(ByVal Img As PictureBox) As Region
        Dim B As Bitmap = Img.Image
        Return GetRegion(B, B.GetPixel(0, 0))
    End Function

    Dim bm_source, bm_dest As Bitmap
    Function ResizeImage(ByVal Img As Image, ByVal H As Integer, ByVal W As Integer) As Image
        Try

            Dim scale_factor As Integer
            Dim img1 As New PictureBox

            scale_factor = Integer.Parse(10)

            bm_source = New Bitmap(Img)
            bm_dest = New Bitmap( _
                CInt(W), _
                CInt(H))

            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

            gr_dest.DrawImage(bm_source, 0, 0, W, H)
            ' tempCnt = True



        Catch ex As Exception

        End Try
        Return (bm_dest)
    End Function

 

Now 
  1. Add a PictureBox to your Form named as PictureBox1
  2. Add Following Code To Crop The Region
PictureBox1.Region = SetRegion(PictureBox1)

0 comments:

Note: only a member of this blog may post a comment.

Translate