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

第一、图片上传,代码如下:
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. Spring MVC学习笔记 01

    applicationcontext.xml的配置 <?xml version="1.0" encoding="UTF-8" ?> <bean ...

  2. excel 批量替换换行符

    在excel批量替换换行符操作步骤: 全选需要查找换行符的范围 CTRL+H调出查找和替换 在查找内容内输入"ctrl+enter"两个组合键 点击查找全部即可. 在excel中输 ...

  3. K - Ancient Messages(dfs求联通块)

    K - Ancient Messages Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  4. SQL 必知必会-- 第1课:数据库基础和什么是SQL

    第1课 了解SQL 1 1.1 数据库基础 11.2 什么是SQL 61.3 动手实践 71.4 小结 8 第一课主要是一些概念,具体笔记如下: 1,数据库(database):保存有组织的数据的容器 ...

  5. [Java] 关键字final、static使用总结

    一.final 根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效率.final ...

  6. java 的文件读取操作

    /** * @param filePath 文件的全路径 * 返回我们读取到的文件内容 * **/ public static String readFile(String filePath) { F ...

  7. 05. 取SQL分组中的某几行数据

    对表中数据分组,有时只需要某列的聚合值:有时却需要返回整行数据,常用的方法有:子查询.ROW_NUMBER.APPLY,总体感觉还是ROW_NUMBER比较直观.测试数据: if OBJECT_ID( ...

  8. Kinect For Windows V2开发日志三:简单的深度读取

    代码示例: #include <Kinect.h> #include <iostream> using namespace std; int main(void) { IKin ...

  9. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted ...

  10. 自定义TabBarController报错 - Unbalanced calls to begin/end appearance transitions for <>

    自定义了TabBarController 之后必须实现以下方法才能避免报错 -(void)viewWillAppear:(BOOL)animated { [self.selectedViewContr ...