473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Cropping an image

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
Apr 23 '06 #1
3 1484
A followup question:

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

-Darrel
Apr 23 '06 #2
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.

"Darrel" <no*****@nospam.com> wrote in message
news:eH****************@TK2MSFTNGP05.phx.gbl...
A followup question:

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

-Darrel

Apr 23 '06 #3
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" <no*****@nospam.com> wrote in message
news:OB****************@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

Apr 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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
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
by: Paul E Collins | last post by:
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?...
2
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
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
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...
0
by: lalithabhamidi | last post by:
Hai i am using GDIplus and working on VC++ 6.0.Please can u help me on Removing red eye and cropping an Image.I immediately need ur help. Thanks in advance for ur help.
1
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.