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
{
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
{
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
{
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
{
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图片上传实例的更多相关文章
- PHP多图片上传实例demo
upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- Thinkphp整合阿里云OSS图片上传实例
Thinkphp3.2整合阿里云OSS图片上传实例,图片上传至OSS可减少服务器压力,节省宽带,安全又稳定,阿里云OSS对于做负载均衡非常方便,不用传到各个服务器了 首先引入阿里云OSS类库 < ...
- PHP结合zyupload多功能图片上传实例
PHP结合zyupload多功能图片上传实例,支持拖拽和裁剪.可以自定义高度和宽度,类型,远程上传地址等. zyupload上传基本配置 $("#zyupload").zyUplo ...
- PHP 多图片上传实例demo
upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- layui加tp5图片上传实例
<div class="layui-fluid"> <div class="layui-row"> <form class=&qu ...
- Asp.NetCoreWebApi图片上传接口(二)集成IdentityServer4授权访问(附源码)
写在前面 本文地址:http://www.cnblogs.com/yilezhu/p/9315644.html 作者:yilezhu 上一篇关于Asp.Net Core Web Api图片上传的文章使 ...
- webuploader项目中多图片上传实例
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- ASP.NET 图片上传工具类 upload image简单好用功能齐全
使用方法: UploadImage ui = new UploadImage(); /***可选参数***/ ui.SetWordWater = "哈哈";//文字水印 // ui ...
- Thinkphp框架图片上传实例
https://www.cnblogs.com/wupeiky/p/5802191.html [原文转载自:https://www.cnblogs.com/guoyachao/p/628286 ...
随机推荐
- java.util Pattern 和 Mathcer
1.测试给定的正则表达式是否匹配输入的字符串,这里该正则表达式只使用一次 private String regex ; private String input; @Before public voi ...
- JavaScript 键盘event.keyCode值列表大全
JavaScript 键盘event.keyCode值列表大全 event.keyCode值列表大全,对于需要根据键盘按键触发相应事件的朋友需要. 网上收集的KeyCode值方便大家查找: k ...
- Linux下C/C++程序开发管理(makefile)
一.引言 从我们刚开始编写一个简单的C/C++ "Hello,World!",到将其编译.运行处结果—这部分工作IDE(集成开发环境)帮我们做了,包括语法错误检查 ...
- 关于JPA方法名创建自动查询
JPA 的根据解析方法名称自动对接口进行实现的方法能节省大量的资源,以下对于解析规则进行列举哈 商品实体类 package com.dionren.zhaoxie.entity.trade; impo ...
- javascript 特性
作用域: javascript的作用域称为静态作用域,在定义语法上就能确认了,而不是运行时. if (true) { var i = 'moersing' } console.log(i); //可以 ...
- UITabBar实现自定义背景及UITabBarItem自定义图片和字体
UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTitle:]; //设置字体颜色(后面设置字体状态)(UITextAttributeTe ...
- Python(2.7.6) 标准日志模块的简单示例
Python 标准库中的 logging 模块提供了一套标准的 API 来处理日志信息的打印. import logging logging.basicConfig( level = logging. ...
- 如何用ASP.NET实现bosh模拟http双向长连接请求
在做研究之前先简单说一下之前公司的通讯模块.最早的时候公司开发的web管理系统是需要配合c++桌面客户端进行一些系统底层操作,并非普通的b/s架构,或者c/s架构,因为需求是可以通过web管理系统向客 ...
- MVC 开启gzip压缩
using System.IO; using System.IO.Compression; using System.Web; using System.Web.Mvc; public class C ...
- .NET调用Java写的WebService
最近遇到一个用.net调用java写的webservice的应用,对方程序员提供了一个后缀为wsdl的文件,这个跟.Net里面生成的wsdl文件差不多,起初没什么概念就查了点资料,知道可以将这个wsd ...