Cropping an image

D

Darrel

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
 
D

Darrel

A followup question:

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

-Darrel
 
K

Kevin Spencer

Image is an abstract class that Bitmap and Metafile are descended from. A
Bitmap is therefore and Image, but an Image is not necessarily a Bitmap.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

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

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top