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)sourceWi dth);
nPercentH = ((float)Height/(float)sourceHe ight);
if(nPercentH < nPercentW) {
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent))/2);
}
else {
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent))/2);
}
int destWidth = (int)(sourceWid th * nPercent);
int destHeight = (int)(sourceHei ght * nPercent);
Bitmap bmPhoto = new Bitmap(Width,
Height, System.Drawing. Imaging.PixelFo rmat.Format24bp pRgb);
bmPhoto.SetReso lution(imgPhoto .HorizontalReso lution,
imgPhoto.Vertic alResolution);
Graphics grPhoto = Graphics.FromIm age(bmPhoto);
grPhoto.Clear(S ystem.Drawing.C olor.White);
grPhoto.Interpo lationMode =
System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c;
grPhoto.DrawIma ge(imgPhoto,
new Rectangle(destX ,destY,destWidt h,destHeight),
new Rectangle(sourc eX,sourceY,sour ceWidth,sourceH eight),
GraphicsUnit.Pi xel);
grPhoto.Dispose ();
// delete the previous user's avatar, both *.jpg and *.gif
if( File.Exists( Page.Server.Map Path(Page.Reque st.ApplicationP ath) +
"UserIcons\ \" + EditUser.Userna me + ".jpg") ){
System.IO.File. Delete( Page.Server.Map Path(Page.Reque st.ApplicationP ath)
+ "UserIcons\ \" + EditUser.Userna me + ".jpg" );
}
if( File.Exists( Page.Server.Map Path(Page.Reque st.ApplicationP ath) +
"UserIcons\ \" + EditUser.Userna me + ".gif") ){
System.IO.File. Delete( Page.Server.Map Path(Page.Reque st.ApplicationP ath)
+ "UserIcons\ \" + EditUser.Userna me + ".gif" );
}
return bmPhoto;
}
--
Alex Mueller
"Arthur Hsu" <ah**@vimatech. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello,
I have an ImageButton that refers to an external image. How can I keep
that image's aspect ratio when I set the ImageButton's size to 120x120?
TIA,
Arthur