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

본문
Imports System.Drawing.Imaging
Imports System.Runtime.CompilerServices
Public Module BitmapExtensions
    <Extension()>
    Public Function TrimImage(ByRef img As Bitmap) As Bitmap
        Dim threshhold = 250
        Dim topOffset = 0
        Dim bottomOffset = 0
        Dim leftOffset = 0
        Dim rightOffset = 0
        Dim x As Long, y As Long, GetColor As Color
        Dim foundColor = False
        ' Get left bounds to crop
        x = 1
        While x < img.Width AndAlso foundColor = False
            y = 1
            While y < img.Height AndAlso foundColor = False
                GetColor = img.GetPixel(x, y)
                If GetColor.R < threshhold OrElse GetColor.G < threshhold OrElse GetColor.B < threshhold Then foundColor = True
                y += 1
            End While
            leftOffset += 1
            x += 1
        End While
        foundColor = False
        ' Get top bounds to crop
        y = 1
        While y < img.Height AndAlso foundColor = False
            x = 1
            While x < img.Width AndAlso foundColor = False
                GetColor = img.GetPixel(x, y)
                If GetColor.R < threshhold OrElse GetColor.G < threshhold OrElse GetColor.B < threshhold Then foundColor = True
                x += 1
            End While
            topOffset += 1
            y += 1
        End While
        foundColor = False
        ' Get right bounds to crop
        x = img.Width - 1
        While x >= 1 AndAlso foundColor = False
            y = 1
            While y < img.Height AndAlso foundColor = False
                GetColor = img.GetPixel(x, y)
                If GetColor.R < threshhold OrElse GetColor.G < threshhold OrElse GetColor.B < threshhold Then foundColor = True
                y += 1
            End While
            rightOffset += 1
            x -= 1
        End While
        foundColor = False
        ' Get bottom bounds to crop
        y = img.Height - 1
        While y >= 1 AndAlso foundColor = False
            x = 1
            While x < img.Width AndAlso foundColor = False
                GetColor = img.GetPixel(x, y)
                If GetColor.R < threshhold OrElse GetColor.G < threshhold OrElse GetColor.B < threshhold Then foundColor = True
                x += 1
            End While
            bottomOffset += 1
            y -= 1
        End While
        '' Create a new image set to the size of the original minus the white space
        ''Dim newImg As Bitmap = New Bitmap(img.Width - leftOffset - rightOffset, img.Height - topOffset - bottomOffset)
        'Dim croppedBitmap As Bitmap = New Bitmap(img)
        'croppedBitmap = croppedBitmap.Clone(New Rectangle(leftOffset - 3, topOffset - 3, img.Width - leftOffset - rightOffset + 6, img.Height - topOffset - bottomOffset + 6), Drawing.Imaging.PixelFormat.DontCare)
        '' Get a graphics object for the new bitmap, and draw the original bitmap onto it, offsetting it do remove the whitespace
        ''Dim g As Graphics = Graphics.FromImage(croppedBitmap)
        ''g.DrawImage(img, 1 - leftOffset, 1 - rightOffset)
        'Return croppedBitmap
        img = img.Clone(New Rectangle(leftOffset - 3, topOffset - 3, img.Width - leftOffset - rightOffset + 6, img.Height - topOffset - bottomOffset + 6), Drawing.Imaging.PixelFormat.DontCare)
        Return img
    End Function
End Module
- 이전글[vb.net] 네이버 스마트 스토어 커머스 API 활용하기 - 주문 조회 23.06.26
- 다음글[vb.net] OpenFileDialog Example 23.06.21
댓글목록
등록된 댓글이 없습니다.





