You could try something like this...
/// <summary>
/// Crop the image if it is too big.
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
protected System.Drawing.Bitmap Crop( System.Drawing.Bitmap bmp ){
System.Drawing.Image imgPhoto = (System.Drawing.Image)bmp;
int Width = 100;
int Height = 100;
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width/(float)sourceWidth);
nPercentH = ((float)Height/(float)sourceHeight);
if(nPercentH < nPercentW) {
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent))/2);
}
else {
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width,
Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(System.Drawing.Color.White);
grPhoto.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
// delete the previous user's avatar, both *.jpg and *.gif
if( File.Exists( Page.Server.MapPath(Page.Request.ApplicationPath) +
"UserIcons\\" + EditUser.Username + ".jpg") ){
System.IO.File.Delete( Page.Server.MapPath(Page.Request.ApplicationPath)
+ "UserIcons\\" + EditUser.Username + ".jpg" );
}
if( File.Exists( Page.Server.MapPath(Page.Request.ApplicationPath) +
"UserIcons\\" + EditUser.Username + ".gif") ){
System.IO.File.Delete( Page.Server.MapPath(Page.Request.ApplicationPath)
+ "UserIcons\\" + EditUser.Username + ".gif" );
}
return bmPhoto;
}