469,943 Members | 2,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,943 developers. It's quick & easy.

Cropping an image

I need to load a bitmap image, crop it, and save it.

By cropping, I don't mean resizing - I mean reducing it to a fixed size
area.

Image img = Bitmap.FromFile("in.bmp");
// what goes here?
img.Save("out.bmp");
img.Dispose();

Code rather than a URL would be ideal, as VB.NET examples do nothing for me.

Thanks.

P.
Nov 15 '05 #1
3 11142
Paul,

What you need to do is create a new instance of the Bitmap class which
starts out as the size that you want to crop down to.

Then, pass the instance of the new image to the static FromImage method
on the Graphics class. This will return a Graphics instance you can use to
draw the cropped image on.

Once you have that, you can call the following overload of DrawImage:

Overloads Public Sub DrawImage( _
ByVal image As Image, _
ByVal destPoints() As Point, _
ByVal srcRect As Rectangle, _
ByVal srcUnit As GraphicsUnit _
)

This will allow you to pass the original Image instance, as well as the
area on it you want to paint, as well as the destination area.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:bs**********@titan.btinternet.com...
I need to load a bitmap image, crop it, and save it.

By cropping, I don't mean resizing - I mean reducing it to a fixed size
area.

Image img = Bitmap.FromFile("in.bmp");
// what goes here?
img.Save("out.bmp");
img.Dispose();

Code rather than a URL would be ideal, as VB.NET examples do nothing for me.
Thanks.

P.

Nov 15 '05 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote:
What you need to do is create a new instance of
the Bitmap class which starts out as the size that
you want to crop down to.
Then, pass the instance of the new image to the
static FromImage method on the Graphics class.
This will return a Graphics instance you can use to
draw the cropped image on.
Once you have that, you can call the following
overload of DrawImage: [...]


Thanks.

However, unlike Image, Graphics doesn't seem to have a method that saves to
a file. Having drawn what I want onto the Graphics object, how can I save
the result?

P.
Nov 15 '05 #3
Paul,

Ahh, you don't have to do anything. The Graphics instance that you drew
into is the Graphics context for the new bitmap you created! Once you are
done drawing (and properly disposing of the graphics object), you can just
call the Save method on the Bitmap/Image instance and save it wherever you
wish.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:bs**********@titan.btinternet.com...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote:
What you need to do is create a new instance of
the Bitmap class which starts out as the size that
you want to crop down to.
Then, pass the instance of the new image to the
static FromImage method on the Graphics class.
This will return a Graphics instance you can use to
draw the cropped image on.
Once you have that, you can call the following
overload of DrawImage: [...]
Thanks.

However, unlike Image, Graphics doesn't seem to have a method that saves

to a file. Having drawn what I want onto the Graphics object, how can I save
the result?

P.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ralph Freshour | last post: by
1 post views Thread by Duncan Smith | last post: by
3 posts views Thread by Darrel | last post: by
2 posts views Thread by Milsnips | last post: by
2 posts views Thread by kumari | last post: by
1 post views Thread by jpuopolo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.