您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp.net编程 >> 正文

asp.net图片上传实例

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 网站后台都需要有上传图片的功能,下面的例子就是实现有关图片上传。缺点:图片上传到本服务器上,不适合大量图片上传

第一、图片上传,代码如下: xxx.aspx  代码如下:  <td class="style1">                  <asp:FileUpload ID="FileUpload1" runat="server"  />                 <asp:Button ID="Button1" runat="server" Text="上传一般图片" onclick="Button1_Click" />              </td>             <td class="style3">                  <asp:Image ID="Image1" runat="server" Height="200px" Width="200px" />              </td>     xxx.aspx.cs 代码如下:  protected void Button1_Click(object sender, EventArgs e)         {             for (int i = 0; i < Request.Files.Count; i++)             {                 HttpPostedFile file = Request.Files[i];                 if (file.ContentLength > 0)                 {                     if (file.ContentType.Contains("image/"))                     {                         using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))                         {                             string FileName = System.IO.Path.GetFileName(file.FileName);                             string[] SplitFileName = FileName.Split('.');                             string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss")+"." + SplitFileName[1];                             img.Save(Server.MapPath("/upload/" + AtterFileName));                               this.Image1.ImageUrl = "upload/" + AtterFileName;                         }                     }                     else                     {                         Response.Write("<script>alert('该文件不是图片格式!');</script>");                     }                 }                 else                 {                     Response.Write("<script>alert('请选择要上传的图片');</script>");                 }               }         }       第二、添加文字水印的图片上传,代码如下: xxx.aspx   代码如下:  <td class="style1">                  <asp:FileUpload ID="FileUpload2" runat="server" />                 <asp:Button ID="Button2" runat="server" Text="上传文字图片" onclick="Button2_Click" />              </td>             <td>                  <asp:Image ID="Image2" runat="server" Height="200px" Width="200px" />              </td>     xxx.aspx.cs 代码如下:  protected void Button2_Click(object sender, EventArgs e)         {             for (int i = 0; i < Request.Files.Count; i++)             {                 HttpPostedFile file = Request.Files[i];                 if (file.ContentLength > 0)                 {                     if (file.ContentType.Contains("image/"))                     {                         using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))                         {                             using (Graphics g = Graphics.FromImage(img))                             {                                 g.DrawString("我的图片", new Font("宋体", 14), Brushes.Red, 0, 0);                             }                             string FileName = System.IO.Path.GetFileName(file.FileName);                             string[] SplitFileName = FileName.Split('.');                             string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];                             img.Save(Server.MapPath("/upload/" + AtterFileName));                             this.Image2.ImageUrl = "upload/" + AtterFileName;                         }                     }                     else                     {                         Response.Write("<script>alert('该文件不是图片格式!');</script>");                     }                 }                 else                 {                     Response.Write("<script>alert('请选择要上传的图片');</script>");                 }               }         }