温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

asp.net图片上传的方法教程

发布时间:2021-10-08 14:59:23 来源:亿速云 阅读:249 作者:iii 栏目:开发技术

本篇内容主要讲解“asp.net图片上传的方法教程”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“asp.net图片上传的方法教程”吧!

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

            }
        }

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


第四、上传图片浓缩图,代码如下:
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图片上传的方法教程”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI