473,506 Members | 17,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11278
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2271
by: Ralph Freshour | last post by:
Is there anyway to crop .jpg images so that they are all of the same width and height? I use the following math to calculate the correct aspect ratio: width / height == aspect ratio so if I...
1
1922
by: Duncan Smith | last post by:
Hello, Can anyone advise me how to crop an EPS image generated by dislin. I can't figure out how to crop the image before generating it. Using PIL to crop the generated image I get an IOError...
3
1488
by: Darrel | last post by:
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...
2
1825
by: Milsnips | last post by:
hi there, i'm uploading to my photo gallery and this is what i'm after.. When user uploads image, i automatically create a 75x75 thumbnail, however using the GetThumbnailImage function drops...
0
1934
by: AmerS | last post by:
hi, I have been trying to generate image patches for a training set but have not succeded yet. The only method i tried is cropping areas of the image and store the cropped images or patches as an...
2
1771
by: kumari | last post by:
Hi, I am facing a problem with generation of thumbnail images in php. The requirement is as follows: the client would provide the image url, description, and content through a page. and the...
1
5632
by: jpuopolo | last post by:
All: I looked around and could not find a simple yet complete example of cropping an image in C#. I had the problem of needing to shave off the bottom-most 18 lines of a directory full of...
1
1678
by: MUHAMMAD RAFIQUE | last post by:
1-How to crop a image in Visual Basic 2- How to save the in the same Jpg file after Cropping
3
2893
by: mcfly1204 | last post by:
I have a photo cropping tool in place that fits my needs, with one exception. When a user uploads a high res. photo, it does not fit on the screen. I have a zoom feature that allows the image to...
0
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.