上传图片生成缩略图

上传图片并生成缩略图,返回上传图片的文件名 #region上传图片并生成缩略图,返回上传图片的文件名
/**////<summary>
///提取上传图片路径
///</summary>
///<paramname="fupImage"></param>
///<returns></returns>

privatestringgetFileName(System.Web.UI.HtmlControls.HtmlInputFilefupImage)
...{
上传正常尺寸的图片#region上传正常尺寸的图片
/**/////上传文件;
//stringpathName="NULL";
//stringnam=fupImage.PostedFile.FileName;
/**/////取得文件名(包括路径)里最后一个"."的索引
//inti=nam.LastIndexOf(".");
/**///取得文件扩展名
//stringnewext=nam.Substring(i).ToLower();
//Randomrnd=newRandom();
//stringnewname=System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Second.ToString()+rnd.Next(1000,9999).ToString()+newext;
//stringthenam=Path.GetFileName(newname);
//stringfilestr=Server.MapPath("../images");
//fupImage.PostedFile.SaveAs(filestr+"/"+thenam);
#endregion


生成缩略图#region生成缩略图
stringpathName=string.Empty;
//生成原图
Byte[]oFileByte=newbyte[fupImage.PostedFile.ContentLength];
System.IO.StreamoStream
=fupImage.PostedFile.InputStream;
System.Drawing.ImageoImage
=System.Drawing.Image.FromStream(oStream);

intoWidth=oImage.Width;//原图宽度
intoHeight=oImage.Height;//原图高度
inttWidth=150;//设置缩略图初始宽度


inttHeight=200;//设置缩略图初始高度

//按比例计算出缩略图的宽度和高度
if(oWidth>=oHeight)
...{
tHeight
=(int)Math.Floor(Convert.ToDouble(oHeight)*(Convert.ToDouble(tWidth)/Convert.ToDouble(oWidth)));
}

else
...{
tWidth
=(int)Math.Floor(Convert.ToDouble(oWidth)*(Convert.ToDouble(tHeight)/Convert.ToDouble(oHeight)));
}


//生成缩略原图
BitmaptImage=newBitmap(tWidth,tHeight);
Graphicsg
=Graphics.FromImage(tImage);
g.InterpolationMode
=System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度字串5
g.Clear(Color.Transparent);//清空画布并以透明背景色填充
g.DrawImage(oImage,newRectangle(0,0,tWidth,tHeight),newRectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel);

stringtFullName=Server.MapPath("../images")+"/"+"t"+DateTime.Now.ToShortDateString().Replace("-","")+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+".jpg";//保存缩略图的物理路径
pathName="t"+DateTime.Now.ToShortDateString().Replace("-","")+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+".jpg";

try
...{
//以JPG格式保存图片
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg);
}

catch(Exceptionex)
...{
throwex;
}

finally
...{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}


#endregion


returnpathName;
}


#endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值