[vb.net] 이미지 주변 여백 제거 > vb.net

본문 바로가기

vb.net

[기타] [vb.net] 이미지 주변 여백 제거

회원사진
하나를하더라도최선을
2023-06-28 08:47 1,461 0

본문



ce888f01ee5a936e000338975409641e_1687909636_7278.png
ce888f01ee5a936e000338975409641e_1687909637_2009.png



 <Extension()>
    Public Function CropWhiteSpace(ByVal bmp As Bitmap) As Bitmap
        Dim w As Integer = bmp.Width
        Dim h As Integer = bmp.Height
        Dim white As Integer = &HFFFFFF
        Dim allWhiteRow As Func(Of Integer, Boolean= Function(r)
                                                           For i As Integer = 0 To w - 1
                                                               If (bmp.GetPixel(i, r).ToArgb() And white) <> white Then Return False
                                                           Next
                                                           Return True
                                                       End Function
 
        Dim allWhiteColumn As Func(Of Integer, Boolean= Function(c)
                                                              For i As Integer = 0 To h - 1
                                                                  If (bmp.GetPixel(c, i).ToArgb() And white) <> white Then Return False
                                                              Next
                                                              Return True
                                                          End Function
 
        Dim topmost As Integer = 0
 
        For row As Integer = 0 To h - 1
            If Not allWhiteRow(row) Then Exit For
            topmost = row
        Next
 
        Dim bottommost As Integer = 0
 
        For row As Integer = h - 1 To 0
            If Not allWhiteRow(row) Then Exit For
            bottommost = row
        Next
 
        Dim leftmost As Integer = 0, rightmost As Integer = 0
 
        For col As Integer = 0 To w - 1
            If Not allWhiteColumn(col) Then Exit For
            leftmost = col
        Next
 
        For col As Integer = w - 1 To 0
            If Not allWhiteColumn(col) Then Exit For
            rightmost = col
        Next
 
        If rightmost = 0 Then rightmost = w
        If bottommost = 0 Then bottommost = h
        Dim croppedWidth As Integer = rightmost - leftmost
        Dim croppedHeight As Integer = bottommost - topmost
 
        If croppedWidth = 0 Then
            leftmost = 0
            croppedWidth = w
        End If
 
        If croppedHeight = 0 Then
            topmost = 0
            croppedHeight = h
        End If
 
        Try
            Dim target = New Bitmap(croppedWidth, croppedHeight)
 
            Using g As Graphics = Graphics.FromImage(target)
                g.DrawImage(bmp, New RectangleF(00, croppedWidth, croppedHeight), New RectangleF(leftmost, topmost, croppedWidth, croppedHeight), GraphicsUnit.Pixel)
            End Using
 
            Return target
        Catch ex As Exception
            Throw New Exception(String.Format("Values are topmost={0} btm={1} left={2} right={3} croppedWidth={4} croppedHeight={5}", topmost, bottommost, leftmost, rightmost, croppedWidth, croppedHeight), ex)
        End Try
    End Function

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색