欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net图片上传实例

程序员文章站 2024-02-28 15:40:34
第一、图片上传,代码如下:xxx.aspx复制代码 代码如下:      &nb...

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

        }