gpt4 book ai didi

asp.net图片上传实例

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章asp.net图片上传实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

第1、图片上传,代码如下: 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>");                 } 。

            }         } 。

  。

第2、添加文字水印的图片上传,代码如下: 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>");                 } 。

  。

            }         } 。

  。

第3、添加图片水印的图片上传,代码如下: xxx.aspx 。

复制代码代码如下:

  。

 <td class="style1">                  <asp:FileUpload ID="FileUpload3" runat="server" />                 <asp:Button ID="Button3" runat="server" Text="上传水印图片" onclick="Button3_Click" />              </td>             <td>                  <asp:Image ID="Image3" runat="server" Height="200px" Width="200px" />              </td> 。

xxx.aspx.cs 。

复制代码代码如下:

protected void Button3_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/"))
                    {
                        string fileName = file.FileName;
                        using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
                        {
                            using (System.Drawing.Image imgWater = System.Drawing.Image.FromFile(Server.MapPath("/img/czlogo.jpg")))
                            {
                                using (Graphics g = Graphics.FromImage(img))
                                {
                                    g.DrawImage(imgWater, 0, 0);
                                }
                                string[] SplitFileName = fileName.Split('.');
                                string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];
                                img.Save(Server.MapPath("/upload/" + AtterFileName));
                                this.Image3.ImageUrl = "upload/" + AtterFileName;
                            }
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('该文件不是图片格式!');</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('请选择要上传的图片');</script>");
                }
            }
        }

第4、上传图片浓缩图,代码如下: xxx.aspx 。

复制代码代码如下:

  。

 <td class="style1">                  <asp:FileUpload ID="FileUpload4" runat="server" />                 <asp:Button ID="Button4" runat="server" Text="上传浓缩图片" onclick="Button4_Click" />              </td>             <td>                  <asp:Image ID="Image4" runat="server" Height="200px" Width="200px" />              </td> 。

xxx.aspx.cs 。

复制代码代码如下:

 protected void Button4_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 (System.Drawing.Image imgThumb = new Bitmap(200, 100))                             {                                 using (Graphics g = Graphics.FromImage(imgThumb))                                 {                                     g.DrawImage(img, new Rectangle(0, 0, imgThumb.Width, imgThumb.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);                                 }                                 string fileName = file.FileName;                                 string[] SplitFileName = fileName.Split('.');                                 string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];                                 img.Save(Server.MapPath("/upload/" + AtterFileName));                                 this.Image4.ImageUrl = "upload/" + AtterFileName;                             }                         }                     }                     else                     {                         Response.Write("<script>alert('该文件不是图片格式!');</script>");                     }                 }                 else                 {                     Response.Write("<script>alert('请选择要上传的图片');</script>");                 }             } 。

  。

        } 。

  。

最后此篇关于asp.net图片上传实例的文章就讲到这里了,如果你想了解更多关于asp.net图片上传实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com