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

第一、图片上传,代码如下:
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
                { www.jbxue.com
                    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
                    { www.jbxue.com
                        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
                    { www.jbxue.com
                        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
                    { www.jbxue.com
                        Response.Write("<script>alert('该文件不是图片格式!');</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('请选择要上传的图片');</script>");
                }
            }

}

asp.net图片上传实例的更多相关文章

  1. PHP多图片上传实例demo

    upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  2. Thinkphp整合阿里云OSS图片上传实例

    Thinkphp3.2整合阿里云OSS图片上传实例,图片上传至OSS可减少服务器压力,节省宽带,安全又稳定,阿里云OSS对于做负载均衡非常方便,不用传到各个服务器了 首先引入阿里云OSS类库 < ...

  3. PHP结合zyupload多功能图片上传实例

    PHP结合zyupload多功能图片上传实例,支持拖拽和裁剪.可以自定义高度和宽度,类型,远程上传地址等. zyupload上传基本配置 $("#zyupload").zyUplo ...

  4. PHP 多图片上传实例demo

    upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  5. layui加tp5图片上传实例

    <div class="layui-fluid"> <div class="layui-row"> <form class=&qu ...

  6. Asp.NetCoreWebApi图片上传接口(二)集成IdentityServer4授权访问(附源码)

    写在前面 本文地址:http://www.cnblogs.com/yilezhu/p/9315644.html 作者:yilezhu 上一篇关于Asp.Net Core Web Api图片上传的文章使 ...

  7. webuploader项目中多图片上传实例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. ASP.NET 图片上传工具类 upload image简单好用功能齐全

    使用方法: UploadImage ui = new UploadImage(); /***可选参数***/ ui.SetWordWater = "哈哈";//文字水印 // ui ...

  9. Thinkphp框架图片上传实例

     https://www.cnblogs.com/wupeiky/p/5802191.html    [原文转载自:https://www.cnblogs.com/guoyachao/p/628286 ...

随机推荐

  1. solr 竞价排行

    在理想的情况下,搜索引擎只返回与用户查询相关的文档.而在现实的查询中,编辑(没发现更合适的表达)通常需要指定特定文档在搜索结果中的特定位置.这样做有很多原因.或许 “置顶” 的文档就是最好的查询结果. ...

  2. Windows XP与Windows 7系统常见漏洞

    1.Windows XP系统常见漏洞 Windows XP系统常见的漏洞有UPNP服务漏洞.升级程序漏洞.帮助和支持中心漏洞.压缩文件夹漏洞.服务拒绝漏洞.Windows Media Player漏洞 ...

  3. MVC+easyui 完整实现

    学习mvc半月有余,趁几天假期,没事做就动手做一个完整的网站玩玩,顺便和上家公司的方法做个比较.页面引擎采用mvc自带的功能,建立视图,交给.net自带渲染出页面(对比:上家公司采用的第三方组件渲染的 ...

  4. 【HTML5 4】《HTML5与CSS3权威指南》 step1 导读

    一.教程重点:以 HTML5和CSS3的新功能和新特性 为导向,辅之以 实战型实例页面 二.内容概况: 第1部分:详细讲解HTML5相关知识,包括 各主流浏览器对HTML5的支持情况. HTML5与H ...

  5. iOS - UI - UIPageControl

    1.UIPageControl 分页控件 //分页控件初始化 UIPageControl * pageControl = [[UIPageControl alloc] init]; //分页页数 pa ...

  6. amoeba实现MySQL读写分离

    amoeba实现MySQL读写分离 准备环境:主机A和主机B作主从配置,IP地址为192.168.131.129和192.168.131.130,主机C作为中间件,也就是作为代理服务器,IP地址为19 ...

  7. python 调用mysql存储过程返回结果集

    存储过程: delimiter | ),)) begin select * from tb_test where mid = imid and user = iuser; end; | delimit ...

  8. hdu 4115 2-SAT判定

    思路:将每个回合的平手和赢最为一对对立状态.那么后面就是2-SAT判断了. #include<iostream> #include<cstdio> #include<al ...

  9. IOS显示九宫格列表

    //总列数 ; CGFloat appW = ; CGFloat appH = ; //间隙 CGFloat maginX = (self.view.frame.size.width - totalC ...

  10. 检测URL地址是否有响应

    今天突然出来了一个问题,URL地址调用导致程序卡死(原因是服务挂了,磁盘坏了) 然后想到了,再调用URL地址前先判断下地址是否有响应,这样不就可以解决问题了吗? C# 代码: /// <summ ...