您好,欢迎访问三七文档
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Drawing;usingSystem.Drawing.Imaging;publicpartialclassAdmin_ThemeSet:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}///summary///上传主题一图片////summary///paramname=sender/param///paramname=e/paramprotectedvoidButton2_Click(objectsender,EventArgse){if(fupPic1.HasFile)//判断里fupPic1是否有此文件地址{string[]strSpil=fupPic1.FileName.Split('.');//将此地址用.号进行分割(img/1.jpg)stringstrEnd=strSpil[strSpil.Length-1].ToLower();//得到后面得jpgstring[]strPic=newstring[]{jpg,gif,bmp,png};//定义一个数组里面放文件格式ArrayListarry=newArrayList();arry.AddRange(strPic);//定义一个可变数组,用于放文件格式if(arry.Contains(strEnd))//判断这个数组中是否有(jpg){Randomrand=newRandom();//定义义个随机数,为了防止你要上传得图片重名StringstrName1=DateTime.Now.ToString(yyyyMMddHmmss)+fupPic1.FileName;//得到不同得名字//StringstrName1=DateTime.Now.ToString(yyyyMMddHmm)+rand.Next(100,9999).ToString()+.+strEnd;//得到不同得名字日期加随即数字StringstrFile=Server.MapPath(~/upfile/);//获取其相对地址fupPic1.SaveAs(strFile+/+strName1);//保存//Image1.ImageUrl=~/upfile/+strName1;//显示此图片//Image1.Visible=true;/*====================================添加代码就在下面这里====2009/6/9测试通过==============================*/stringserverpath=strFile+strName1;StringstrName2=DateTime.Now.ToString(yyyyMMddHmmss)+[1]+fupPic1.FileName;//缩略图名字stringthumbnailpath=strFile+strName2;stringoutthumbnailPath=;MakeThumbnail(serverpath,thumbnailpath,739,98,outoutthumbnailPath);Image1.ImageUrl=~/upfile/+strName2;/*====================================添加代码就在上面这里==================================*//*SqlConnectioncon=newSqlConnection(server=(local);userid=sa;pwd=wangjingyi;Database=grand);con.Open();SqlCommandcom=newSqlCommand(INSERTINTOimage(imagename,url)values('+strName1+','+strFile+'),con);com.ExecuteNonQuery();*/}else{Response.Write(scriptalert('不是标准的图片格式')/script);}}else{Response.Write(scriptalert('不存在此文件')/script);}}///summary///修改数据库////summaryprotectedvoidUpdateData(stringPicUrl){Maticsoft.BLL.Themebll=newMaticsoft.BLL.Theme();Maticsoft.Model.Thememodel=newMaticsoft.Model.Theme();}///summary///提交主题一链接////summary///paramname=sender/param///paramname=e/paramprotectedvoidAdd_Btn_Click(objectsender,EventArgse){Maticsoft.BLL.Themebll=newMaticsoft.BLL.Theme();Maticsoft.Model.Thememodel=newMaticsoft.Model.Theme();model.Id=1;}///summary///上传主题二图片////summary///paramname=sender/param///paramname=e/paramprotectedvoidButton3_Click(objectsender,EventArgse){Maticsoft.BLL.Themebll=newMaticsoft.BLL.Theme();Maticsoft.Model.Thememodel=newMaticsoft.Model.Theme();model.Id=1;}///summary///提交主题二链接////summary///paramname=sender/param///paramname=e/paramprotectedvoidButton1_Click(objectsender,EventArgse){Maticsoft.BLL.Themebll=newMaticsoft.BLL.Theme();Maticsoft.Model.Thememodel=newMaticsoft.Model.Theme();model.Id=1;}///summary///上传主题图片三////summary///paramname=sender/param///paramname=e/paramprotectedvoidButton4_Click(objectsender,EventArgse){Maticsoft.BLL.Themebll=newMaticsoft.BLL.Theme();Maticsoft.Model.Thememodel=newMaticsoft.Model.Theme();model.Id=1;}///summary///提交主题三链接////summary///paramname=sender/param///paramname=e/paramprotectedvoidButton5_Click(objectsender,EventArgse){Maticsoft.BLL.Themebll=newMaticsoft.BLL.Theme();Maticsoft.Model.Thememodel=newMaticsoft.Model.Theme();model.Id=1;}///summary///生成缩略图////summary///paramname=originalImagePath源图路径(物理路径)/param///paramname=thumbnailPath缩略图路径(物理路径)/param///paramname=width缩略图宽度/param///paramname=height缩略图高度/param///paramname=mode生成缩略图的方式/param///paramname=outthumbnailPath返回缩略图的/parampublicvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,outstringoutthumbnailPath){System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);inttowidth=width;inttoheight=height;intx=0;inty=0;intow=originalImage.Width;intoh=originalImage.Height;//{//caseHW://指定高宽缩放(可能变形)//break;//caseW://指定宽,高按比例//toheight=originalImage.Height*width/originalImage.Width;//break;//caseH://指定高,宽按比例//towidth=originalImage.Width*height/originalImage.Height;//break;//caseCut://指定高宽裁减(不变形)if((double)originalImage.Width/(double)originalImage.Height(double)towidth/(double)toheight){oh=originalImage.Height;ow=originalImage.Height*towidth/toheight;y=0;x=(originalImage.Width-ow)/2;}else{ow=originalImage.Width;oh=originalImage.Width*height/towidth;x=0;y=(originalImage.Height-oh)/2;}//break;//default://break;//}//新建一个bmp图片System.Drawing.Imagebitmap=newSystem.Drawing.Bitmap(towidth,toheight);//新建一个画板Graphicsg=System.Drawing.Graphics.FromImage(bitmap);//设置高质量插值法g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量,低速度呈现平滑程度g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;//清空画布并以透明背景色填充g.Clear(Color.Transparent);//在指定位置并且按指定大小绘制原图片的指定部分g.DrawImage
本文标题:C#生成缩略图
链接地址:https://www.777doc.com/doc-3894176 .html