tags:

views:

53

answers:

3

I'm using a web cam that takes 320 X 240 pictures. This sort of gives the picture a "landscape" look to them, and I need to re-size the pictures to a 169 X 225 "portrait" picture. Its for a badge program. Anyway, when I re-size the pictures, they get all scrunched up because of the difference in aspect ratio. Is there a way to re-size the image and the aspect ratio at the same time? Would this be considered cropping or resizing? I'm a little confused on where to begin. I mainly use VB, but if someone has a good example in C#, ill take the time to convert it. Thanks

+3  A: 

Changing the aspect ratio and cropping are the same thing. You can calculate the new portrait dimensions like so: (ratio based on standard 3 x 5 photo size)

Double aspectRatio = 3.0 / 5.0
Int newHeight = actualHeight ' Use full height
Int newWidth = actualWidth * aspectRatio

and you want a center crop so you have to calculate that like so:

Int cropY = 0 ' Use full height
Int cropX = actualWidth / 2.0 - newWidth / 2.0

The Bitmap object in System.Drawing has a Clone method which accepts a cropping rectangle as a parameter. The Graphics object has a DrawImage method that you can use to resize and crop the image simultaneously.

BC
Am I looking for a cropping method in the bitmap class or something? If i just set the Height and Width, wont I still have the same problem?
broke
@broke Answer modified
BC
Nevermind figured it out thank you!
broke
A: 

http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-rotate

private  Bitmap rotateImage(Bitmap b, float  angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//move rotation point to center of image
g.TranslateTransform((float)b.Width/2, (float)b.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)b.Width/2,-(float)b.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}"




private  static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);

if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();

return (Image)b;
}



private  static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
+2  A: 

You have two choices. You can just crop, since the final size is so close to the original, or you can crop and resize. If you crop then resize, you'll want to crop to 180x240 to get to the proper aspect ratio; when you resize that result to 169x225 there won't be any distortion.

Here's a quick link I found to describe cropping in VB.NET: http://www.nerdydork.com/crop-an-image-bitmap-in-c-or-vbnet.html

Mark Ransom
This is what i ended up using. Thanks!
broke