Darrel
2006-04-24 03:34:22 AM

I've been trying to figure out how to crop an image. I found the sample code
below, but I'm not exactly sure what it's doing. I've added my own comments
as best I can trying to explain what I think it's doing. Can anyone clarify
this for me?

' I think this takes my current image (g) and then resizes it to what I want
bmpImage = New Bitmap(g, newWidth, newHeight)

' now I set up a rectangle to the dimensions of the cropped version
recCrop = New Rectangle(0, 0, resizedWidth, resizedHeight)

' this is where I begin to get lost. I think this is taking
' my resizedi mages and cropping it based on the rectangle
' dimensions I made
bmpCrop = New Bitmap(recCrop.Width, recCrop.Height, bmpImage.PixelFormat)

' Now I get really lost, I'm not sure what the rest of this is doing.
gphCrop = Graphics.FromImage(bmpCrop)
recDest = New Rectangle(0, 0, Width, Height)
gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width,
_recCrop.Height, GraphicsUnit.Pixel)

-Darrel


 

Re: Cropping an image

Darrel
2006-04-24 04:06:00 AM

A followup question:

What's the difference between system.drawing.image and
system.drawing.bitmap?

-Darrel


 



Re: Cropping an image

Kevin Spencer
2006-04-24 05:33:00 AM

Okay, the Graphics class enables you to draw on a surface, such as the Image
you just created at the new size. The overload you need (assuming .Net 2.0)
is:

Graphics.DrawImage Method (Image, Rectangle, Rectangle, GraphicsUnit)
The first argument is the source image (the one you want to crop).
The second is the destination Rectangle (the rectangle that defines bmpCrop)
The third is the source rectangle (the portion of the source image you want
to capture)
The fourth is a GarphicsUnit, which would be GraphicsUnit.Pixel in this
case.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Darrel" <notreal@nospam.com>wrote in message
news:OB%23600wZGHA.2376@TK2MSFTNGP03.phx.gbl...
>I've been trying to figure out how to crop an image. I found the sample
>code below, but I'm not exactly sure what it's doing. I've added my own
>comments as best I can trying to explain what I think it's doing. Can
>anyone clarify this for me?
>
>' I think this takes my current image (g) and then resizes it to what I
>want
>bmpImage = New Bitmap(g, newWidth, newHeight)
>
>' now I set up a rectangle to the dimensions of the cropped version
>recCrop = New Rectangle(0, 0, resizedWidth, resizedHeight)
>
>' this is where I begin to get lost. I think this is taking
>' my resizedi mages and cropping it based on the rectangle
>' dimensions I made
>bmpCrop = New Bitmap(recCrop.Width, recCrop.Height, bmpImage.PixelFormat)
>
>' Now I get really lost, I'm not sure what the rest of this is doing.
>gphCrop = Graphics.FromImage(bmpCrop)
>recDest = New Rectangle(0, 0, Width, Height)
>gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width,
>_recCrop.Height, GraphicsUnit.Pixel)
>
>-Darrel
>