111,129
社区成员
发帖
与我相关
我的任务
分享
<img src='/cenep/biad/img.aspx?attachPath=/imgUpload/img_jnt.jpg&maxwidth=100' border='0'/>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 获取图片路径
string attachPath = Request.QueryString["attachPath"].ToString();
// 初始化最大宽度和高度
float maxwidth = 0;
float maxheight = 0;
// 获取传过来的最大宽度
if (Request.QueryString["maxwidth"] != null)
{
maxwidth = float.Parse(Request.QueryString["maxwidth"].ToString());
}
// 获取传过来的最大高度
if (Request.QueryString["maxheight"] != null)
{
maxheight = float.Parse(Request.QueryString["maxheight"].ToString());
}
// 调用图片出来函数
BreviaryImage(Server.MapPath(attachPath), maxwidth, maxheight);
}
}
/// <summary>
/// 图片处理
/// </summary>
/// <param name="ImagePath">图片服务器路径</param>
/// <param name="maxwidth">最大宽度</param>
/// <param name="maxheight">最大高度</param>
protected void BreviaryImage(string ImagePath, float maxwidth, float maxheight)
{
// 获取图片对象
using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath))
{
// 取得图片名字
string imageName = ImagePath.Substring(ImagePath.LastIndexOf("\\") + 1);
float width = image.Width;
float height = image.Height;
// 图片宽高比例
float widthheight = width / height;
// 图片高宽比例
float heightwidth = height / width;
// 新图片的宽高
float newwidth = 0;
float newheight = 0;
if (maxwidth == 0)
{
maxwidth = width;
}
if (maxheight == 0)
{
maxheight = height;
}
// 宽超过设置的最大宽度时
if (width > maxwidth)
{
newwidth = maxwidth;
newheight = maxwidth / heightwidth;
}
// 高超过设置的最大高度时
if (height > maxheight)
{
newheight = maxheight;
newwidth = maxheight / widthheight;
}
// 缩略图目录
string FilePath = "/BreviaryImage/";
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
// 保存缩略图
using (System.Drawing.Image aNewImage = image.GetThumbnailImage(Convert.ToInt32(newwidth), Convert.ToInt32(newheight), null, IntPtr.Zero))
{
aNewImage.Save(Server.MapPath(FilePath) + imageName);
}
}
}string FilePath = "/BreviaryImage/";