问题描述
请问,网站在处理上传图片时,是怎样来控制图片的大小的,(怎样把照片或大图片缩成几K,是不是要先把原图接受到服务器后在处理?)
解决方案
解决方案二:
上传到服务器然后生成缩略图
解决方案三:
那业已就是说还是得把文件上传上来,然后处理,生产缩略图,再删掉原来的图片?正规的网站是怎样实现的
解决方案四:
我刚把我的网站上写了这个给你代码你看一下就明白了;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.Data.SqlClient;usingSystem.IO;usingDB;usingSystem.Data.SqlClient;publicpartialclassUser_UpLoadFile:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidUPbutton_Click(objectsender,EventArgse){if(FileUpload1.HasFile){stringfileContentType=FileUpload1.PostedFile.ContentType;if(fileContentType=="image/bmp"||fileContentType=="image/gif"||fileContentType=="image/pjpeg"){stringname=FileUpload1.PostedFile.FileName;FileInfofile=newFileInfo(name);stringSavePath="/upload/";stringfileExes=(System.IO.Path.GetExtension(name)).ToString().ToLower();Randomran=newRandom();intRandKey=ran.Next(9,999);stringfileName="P"+System.DateTime.Now.ToString("yyMMddhhmm")+RandKey+fileExes;stringfileName_s="S"+fileName.Substring(1);stringwebFilePath=Server.MapPath(SavePath+fileName);stringwebFilePath_s=Server.MapPath(SavePath+fileName_s);if(!File.Exists(webFilePath)){try{FileUpload1.SaveAs(webFilePath);this.Image1.Visible=true;this.Image1.ImageUrl=SavePath+fileName_s;MakeThumbnail(webFilePath,webFilePath_s,99,113,"Cut");//生成缩略图方法stringBigimg=SavePath+fileName;stringSmallimg=SavePath+fileName_s;DataInsert(Bigimg,Smallimg);error.Text="提示:上传成功,生成缩略图大小:"+FileUpload1.PostedFile.ContentLength/1024+"K";}catch(Exceptionex){error.Text="提示:文件上传失败,失败原因:"+ex.Message;}}else{error.Text="提示:文件已经存在,请重命名后上传";}}else{error.Text="提示:文件格式不符,只允许:.jpg/gif";}}}publicstaticvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode){System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);inttowidth=width;inttoheight=height;intx=0;inty=0;intow=originalImage.Width;intoh=originalImage.Height;switch(mode){case"HW"://指定高宽缩放(可能变形)break;case"W"://指定宽,高按比例toheight=originalImage.Height*width/originalImage.Width;break;case"H"://指定高,宽按比例towidth=originalImage.Width*height/originalImage.Height;break;case"Cut"://指定高宽裁减(不变形)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;}System.Drawing.Imagebitmap=newSystem.Drawing.Bitmap(towidth,toheight);System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(bitmap);g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;g.Clear(System.Drawing.Color.Transparent);g.DrawImage(originalImage,newSystem.Drawing.Rectangle(0,0,towidth,toheight),newSystem.Drawing.Rectangle(x,y,ow,oh),System.Drawing.GraphicsUnit.Pixel);try{bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);}catch(System.Exceptione){throwe;}finally{originalImage.Dispose();bitmap.Dispose();g.Dispose();}}protectedvoidDataInsert(stringBigimage,stringSmlimage){ConnDBdb=newConnDB();//这里是我的数据库连接,你改一下try{stringUserName=Session["UserName"].ToString();stringsql="insertintoUser_Photo(User_name,Photo_big,Photo_small)values('"+UserName+"','"+Bigimage+"','"+Smlimage+"')";db.ExecSql(sql);}catch(SqlExceptionerror){throw(error);}}}HTML页面代码:<body><formid="form1"runat="server"><div><tablestyle="width:383font-family:宋体,Arial,Helvetica,sans-serif;font-size:12px;border-spacing:0px;border-collapse:0;caption-side:0;empty-cells:0"><tr><tdalign="center"><asp:ImageID="Image1"runat="server"BorderColor="#EEEEEE"BorderStyle="Solid"BorderWidth="5px"Height="113px"ImageUrl="#"Visible="False"Width="99px"/></td></tr></table><tablestyle="font-family:宋体,Arial,Helvetica,sans-serif;font-size:12px;border-spacing:0px;border-collapse:0;caption-side:0;empty-cells:0"class="style10"><tr><tdclass="style8"><spanlang="zh-cn">选择照片:</span></td><tdclass="style9"><asp:FileUploadID="FileUpload1"runat="server"BackColor="White"BorderColor="Silver"BorderStyle="Solid"BorderWidth="1px"Font-Overline="False"Font-Size="12px"Font-Strikeout="False"ForeColor="#333333"Width="240px"/></td><td><asp:ButtonID="UPbutton"runat="server"Text="上传"Width="64px"CssClass="inputx"Height="23px"onclick="UPbutton_Click"/></td></tr></table><tablewidth="368px"style="font-family:宋体,Arial,Helvetica,sans-serif;font-size:12px;border-spacing:0px;border-collapse:0;table-layout:0;caption-side:0;empty-cells:0"><tr><td> </td><tdclass="style7"align="Right"> </td><tdstyle="height:23px;"><asp:LabelID="error"runat="server"Font-Size="12px"ForeColor="#FF0066"></asp:Label></td></tr><tr><td> </td><tdclass="style7"align="Right"> </td><tdstyle="height:23px;"> </asp:Label><inputid="Button1"class="style11"type="button"value="关闭窗口"onclick="winclose()"/></td></tr></table></div></form></body>
解决方案五:
你直接把我上面的代码拷到本机调试一下就OK了
解决方案六:
好的,多谢
解决方案七:
给分呀????