May 1, 2010 06:43 PM
(This post was last modified: May 2, 2010 02:14 PM by codepoet.)
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.
If You Like this tutorial, please check out my other tutorials by clicking ==> here
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 FunctionIf You Like this tutorial, please check out my other tutorials by clicking ==> here
PHP & .NET Contributor.


