posts - 116,  comments - 34,  trackbacks - 0

//定义image类的对象
  System.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 btnUp_Click(object sender, System.EventArgs e)
  {
   string mPath;

   if("" != upImage.PostedFile.FileName)
   {
    imagePath= upImage.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("upFile");
      //保存到虚拟路径
      upImage.PostedFile.SaveAs(mPath+"\\"+imageName);
      //显示原图
      imageSource.ImageUrl = "upFile/"+imageName;
      //为上传的图片建立引用
      image=System.Drawing.Image.FromFile(mPath+"\\"+imageName);   
      //生成缩略图
      newimage=image.GetThumbnailImage(100,100,callb,new System.IntPtr());
      //把缩略图保存到指定的虚拟路径
      newimage.Save(Server.MapPath("upFile")+"\\small"+imageName);
      //释放image对象占用的资源
      image.Dispose();
      //释放newimage对象的资源
      newimage.Dispose();
      //显示缩略图
      imageSmall.ImageUrl = "upFile/"+"small"+imageName;

      Response.Write("上传成功!");
     }
     catch
     {
      Response.Write("上传成功!");
     }
     
    }
   }
   
  }

posted on 2006-04-28 16:28 萌芽的叶子 阅读(127) 评论(0)  编辑 收藏 引用 所属分类: asp.net
只有注册用户登录后才能发表评论。

<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(3)

随笔分类(115)

随笔档案(116)

文章分类(4)

相册

收藏夹(78)

.net中文社区

.net博客

.net英文社区

AJAX

ASP.NET 2.0

ASP.NET 学习

DataBase

ERP

E杂志

Html&Css

JavaScript

Microsoft

Open Sourse

SAP

WebCasts

WebServices

XML

其他

好友Blog

好文章连接

开发工具

控件

物流

职业经理人

设计模式

读书网站

非技术

项目管理

搜索

  •  

积分与排名

  • 积分 - 57075
  • 排名 - 108

最新评论

阅读排行榜

评论排行榜