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 ...
随机推荐
- DM 之 全解析
一.设计模式的分类 二十三大设计模式,分为三大类: 1. 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 2. 结构型模式,共七种:适配器模式.装饰器模式.代理模式. ...
- Solr特殊字符转义处理
做站内搜索时,如果输入的参数中包含英文冒号.双引号或其他具有特殊含义的字符时,可能需要做转义来避免查不到数据的问题. 测试于:Solr 4.5.1, Jdk 1.6.0_45, Tomcat 6.0. ...
- 【阿里云产品公测】ACE安装wordpress博客图文教程
作者:阿里云用户51干警网 阿里云ace搭建wordpress图文教程 按照大大说的,wordpress确实能够轻松创建,只有几步. 我想说,小白的世界技术大大还是不了解.想当初我了解一下怎么 ...
- 【Android Studio使用教程4】Android Studio下载
鉴于Android官网上下载很慢,Android Studio等已在网盘分享:Android Studio 网盘下载路径: windows:http://yunpan.cn/cfTszP2wrJxdD ...
- NSURLConnection & NSRULSession
NSURLConnection & NSRULSession NSURLSession是NSURLConnection 的替代者,在2013年苹果全球开发者大会(WWDC2013)随ios7一 ...
- [Android]天气App 1
闲赋在家,无事可做就想着做点东西,于是乎把玩手机,我最常用的就是看天气,基本上我每天起来第一件事就是看天气,哈哈,用别人的这么爽,为什么不自己整一个关于天气的应用呢,墨迹天气.小米系统自带的天气.ya ...
- JavaScript break和continue 跳出循环
在JavaScript中,使用 break 和 continue 语句跳出循环: break语句的作用是立即跳出循环,即不再执行后面的所有循环: continue语句的作用是停止正在执行的循环,直接进 ...
- Laravel-Administrator enum使用数字key
参考连接:Enum filter with numeric values 修改Fields\Enum::build()方法 $options['options'][] = array( 'id' =& ...
- Macbook之用brew安装Python
1. brew install python 2.If you don't have ~/.bash_profile, add ~/.bash_profile by touch ~/.bash_pro ...
- CS加密算法
概述: 加密数据可以使用对称加密或非对称加密算法,使用对称加密比非对称密钥快得多,但对称密钥需要解决安全交换密钥的问题.在 .NET Framework中,可以使用System.Security.Cr ...