Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.NET] Auto Crop Image

originally posted on dotnet-snippets.com

Description: you can pass an image to and return a dynamically cropped version of the image without the excess space.
Code
Function Crop(ByVal p As Image) As Image
    Dim x, y As Int32 'for use of X,Y Coordinates of pixels
    Dim b As New Bitmap(p) 'image needed to crop
    Dim c As New Color() 'pixel color for use of identifying if background
    
    Dim intLeft As Int32 'furthest left X coordinate
    Dim intRight As Int32 'furthest right X coordinate
    Dim intBottom As Int32 'furthest to the bottom Y coordinate
    Dim intTop As Int32 'furthest to top Y coordinate
    y = 0
    Do Until y = b.Height 'loop through pixels on Y axis until end of image height
    x = 0
    Do Until x = b.Width 'loop through pixels on X axis until end of image width
    c = b.GetPixel(x, y) 'Get the color of the pixel
    'First we want to establish if the pixel is White (the color of our background)
    If c.R <> 0 And c.G <> 0 And c.B <> 0 Then
    'We'll also want to ignore colors close to the background color
    'I did this because some of the images I had were scanned, and
    'the paper color didn't always scan at a perfect white
    If c.R < 250 Or c.G < 250 Or c.B < 250 Then
    'Determine if pixel is further left than the value we already have
    If intLeft <= 0 And intLeft < x Then
    intLeft = x
    End If
    'Determine if pixel is further to the top than the value we already have
    If intTop <= 0 And intTop < y Then
    intTop = y
    End If
    'Determine if pixel is further right than the value we already have
    If intRight <= b.Width And intRight < x Then
    intRight = x
    End If
    'Determine if pixel is further to the bottom than the value we already have
    If intBottom <= b.Height And intBottom < y Then
    intBottom = y
    End If
    End If
    End If
    x += 1
    Loop
    y += 1
    Loop
    Dim intNewWidth As Int32 = (intRight) 'Establish width of new cropped image
    Dim intNewHeight As Int32 = (intBottom) 'Establish height of new cropped image
    'Create new image with a 10 pixel buffer, so image won't be too close to edge
    Dim imgCropped As New Bitmap(intNewWidth + 10, intNewHeight + 10)
    'Create a Graphics object for drawing to the cropped image
    Dim objGraphics As Graphics = Graphics.FromImage(imgCropped)
    'set the background color to white (you can choose what you like
    objGraphics.Clear(System.Drawing.Color.White)
    'Determine where you'd like to start drawing from the Top (Y axis)
    Dim intStartTop As Int32 = 5 '-intTop / 40 + 5
    'Determine where you'd like to start drawing from the Left (X axis)
    Dim intStartLeft As Int32 = 5 '-intLeft / 40 + 5
    'you want to start drawing the image at the point where an actual image exists,
    'not just blank space. I negated the furthest top and left pixel points and
    'divided them by 40 and added 5 (half of the 10 pixel buffer, so 5x on each side)
    'The reason I divided was because the image would be too far to the left or top
    'sometimes and 40 just happened to work the best for the images I was cropping.
    'I believe this has to do with the horizontal an vertical resolution values,
    'bit depth, and image format
    'There must be room for improvement or a way to do this better, but it worked for
    'what I needed.
    'Draw the original image to your new cropped sized image
    objGraphics.DrawImage(b, intStartLeft, intStartTop)
    b.Dispose()
    objGraphics.Dispose()
    'return the Cropped image to the caller
    Return imgCropped
    End Function

If You Like this tutorial, please check out my other tutorials by clicking ==> here
PHP & .NET Contributor.
Managed image processing, ouch. Looks good though but just a heads up for anyone who uses this - I don't believe it was intended to quickly process multiple images.
its really not...but in the project i used it in, it was allot simple....
PHP & .NET Contributor.
It's originally from pscode.com, it was a pretty for a pretty specific task and isn't very versatile (it only works if you want to trim a white edge. You could speed it up considerably. The code is pretty weird.
(May 1, 2010 07:55 PM)julian Wrote:  It's originally from pscode.com, it was a pretty for a pretty specific task and isn't very versatile (it only works if you want to trim a white edge. You could speed it up considerably. The code is pretty weird.

NO!! 😠 it was originally posted on my website the leached!!! get the Right
PHP & .NET Contributor.
crow407 is right, it was on his web site first, later it was leached and posted on pscode.com
I was just googling to see the scenario where it was used and commented in case any one else was wondering. The project was posted on pscode in 2003, with the comment " I needed to do this at work, where we had logos in our information system." You have your age down as 18 on the forums. So you wrote it for work, aged 11. Whatever, I really don't care.
Well, you should at least give a credit or maybe a link. I don't really like copy and paste people, no offence through
(May 2, 2010 07:56 AM)julian Wrote:  I was just googling to see the scenario where it was used and commented in case any one else was wondering. The project was posted on pscode in 2003, with the comment " I needed to do this at work, where we had logos in our information system." You have your age down as 18 on the forums. So you wrote it for work, aged 11. Whatever, I really don't care.

hahahaha....what makes you think i give my right age at a HACKING forum? the more info you give the less safe you become!
PHP & .NET Contributor.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by C++Dec 29, 2019 06:16 PM 16 827 Dec 29, 2019 06:16 PM
Last Post: DNA
[Python] by DistrictApr 7, 2019 03:14 AM 4 227 Apr 7, 2019 03:14 AM
Last Post: lockhead
[.NET] by D3n1s1990Oct 18, 2017 10:26 PM 0 211 Oct 18, 2017 10:26 PM
Last Post: D3n1s1990