代码如下:
| //定义image类的对象
Drawing.Image image,newimage; //图片路径 protected string imagePath; //图片类型 protected string imageType; //图片名称 protected string imageName; //提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行 //如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false System.Drawing.Image.GetThumbnailImageAbort callb = null; private void sm_Click(object sender, System.EventArgs e) { string mPath; if("" != File1.PostedFile.FileName) //File1为上传文件控件 { imagePath = File1.PostedFile.FileName; //取得图片类型 imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1); //取得图片名称 imageName = imagePath.Substring(imagePath.LastIndexOf("\")+1); //判断是否是JPG或者GIF图片,这里只是举个例子,并不一定必须是这两种图片 if("jpg" != imageType &&"gif" != imageType) { Response.Write("<script language='javascript'> alert('对不起!请您选择jpg或者gif格式的图片!');</script>"); return; } else { try { //建立虚拟路径 mPath=Server.MapPath("UploadFiles"); //保存到虚拟路径 File1.PostedFile.SaveAs(mPath+"\"+imageName); //显示原图, imageSource为图片控件 //imageSource.ImageUrl = "UploadFiles/"+imageName; //为上传的图片建立引用 image=System.Drawing.Image.FromFile(mPath+"\"+imageName); //生成缩略图 newimage=image.GetThumbnailImage(200,200,callb,new System.IntPtr()); |

