上传图片并生成缩略图

前台
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
                  <table id="Table1" cellpadding="1" cellspacing="1" width568 border="1">
                        <tr>
                              <td>
                                    <asp:Label ID="Label1" Runat="server">要上传的图片</asp:Label>
                              </td>
                              <td>
                                    <input id="upImage" type="file" name="File1" runat="server"></td>
                              <td><asp:Button ID="btnUp" Runat="server" Text="上传并生成缩图"></asp:Button></td>
                        </tr>
                        <tr>
                              <td><asp:Label ID="Label2" Runat="server">原图片</asp:Label></td>
                              <td align="center" colspan="2"><asp:Image ID="imageSource" Runat="server"></asp:Image>
                              </td>
                        </tr>
                        <tr>
                              <td><asp:Label ID="Label3" Runat="server">缩图</asp:Label></td>
                              <td align="center" colspan="2">
                                    <asp:Image ID="imageSmall" Runat="server"></asp:Image></td>
                        </tr>
                  </table>
            </form>
后台
public System.Drawing.Image image,newimage;//定义
         protected string imagePath;
  protected string imageType;
  protected string imageName;
  //提供一个回调方法,用于确定Image对象在执行生成缩图操作时河时提前取消执行
  //如果此方法确定GetThumbnailImage方法应该提前停此执行,返回true 否则返回false
  protected 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.IndexOf("\\")+1);
    if("jpg"!=imageType&&"gif"!=imageType)
    {
     Response.Write("<script laguage='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(300,300,callb,new System.IntPtr());
      //把缩图保存到指定的虚拟路径
      newimage.Save(Server.MapPath("upFile")+"\\small"+imageName);
      //释放image对象占用的资源
      image.Dispose();
      newimage.Dispose();

      imageSmall.ImageUrl="upFile/"+"small"+imageName;
      this.Response.Write("上传成功");

     }
     catch
     {
      this.Response.Write(" 上传失败");
     }
    }
   }
  
  }

转载于:https://www.cnblogs.com/yefei520/archive/2006/01/05/311343.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值