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

본문


    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(0, 0, 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
- 이전글[vb.net] Mouse, Keyboard 이벤트를 사용하여 해당 포커스 캡처 23.07.09
- 다음글[vb.net] 네이버 스마트 스토어 커머스 API 활용하기 - 주문 조회 23.06.26
댓글목록
등록된 댓글이 없습니다.





