473,657 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.PixelF ormat)

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

-Darrel
Apr 23 '06 #1
3 1495
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******** ********@TK2MSF TNGP05.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.DrawIm age 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.Pi xel 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******** ********@TK2MSF TNGP03.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.PixelF ormat)

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

-Darrel

Apr 23 '06 #4

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

Similar topics

1
2279
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 need a 268 w x 176 h I do this: target width 268 / aspect ration == correct height
1
1933
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 (apparently because I'm using Windows). I could use other file formats, but I'm not too keen on the results I'm getting from using e.g. .tif. Basically I need to copy (publication quality) images created in dislin and paste them into word...
3
11297
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? img.Save("out.bmp"); img.Dispose();
2
1833
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 the quality and distorts it, so what i would like to do is: resize the original image to: Width: 75px
0
1948
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 array of bytes. The problem is, same patch gets stored thoughout the array, meaning its cropping the same area in the loop and the rectangle is not moving, could some one explain to me why, Thank You. heres the code: MemoryStream stream = new...
2
1781
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 same information will be stored in database. While displaying all images i should take that url. and read it byte by byte and pass to a curl function where in it crops and echos.. the cropped version of image.
0
1194
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
5665
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 images. The following code does this. I hope that this serves as a concise yet complete example of how to tackle cropping an image. --- Note that inputDireName and outputDirName have been set in the class
1
1699
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
2902
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 fit on the screen, but then the user is cropping the downsized image and not the original. I would like to find a way to have the cropped area on the original image correspond with the cropped area on the zoomed image. Any thoughts? I have...
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1732
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.