假定在Image控件中想把图片的宽度显示为固定值600,超出600的压缩显示宽高,不足600的原图显示:
FileStream fs = new FileStream(Server.MapPath(txtPicUrl), FileMode.Open, FileAccess.Read); --txtPicUrl为图片在服务器上的物理路径如:~\Pic
System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
int width = Convert.ToInt32(image.Width.ToString());
double height = Convert.ToInt32(image.Height.ToString());
if (width > 600)
{
height = Math.Round(height * 600 / width);
}
this.img.ImageUrl =txtPicUrl;
this.img.Width = 600;
this.img.Height = (int)height;
img为.aspx文件界面中Image控件的ID
FileStream fs = new FileStream(Server.MapPath(txtPicUrl), FileMode.Open, FileAccess.Read); --txtPicUrl为图片在服务器上的物理路径如:~\Pic
System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
int width = Convert.ToInt32(image.Width.ToString());
double height = Convert.ToInt32(image.Height.ToString());
if (width > 600)
{
height = Math.Round(height * 600 / width);
}
this.img.ImageUrl =txtPicUrl;
this.img.Width = 600;
this.img.Height = (int)height;
img为.aspx文件界面中Image控件的ID
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26189725/viewspace-1432961/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26189725/viewspace-1432961/