代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
简单的上传图片并生成缩略图
作者:
Dezai.CN
/ 发布于
2011/4/26
/
659
<div> <div id="cnblogs_post_body"> 页面: <div class="cnblogs_code"> <pre><div><span style="color: #000000"><</span><span style="color: #000000">form id</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">form1</span><span style="color: #800000">"</span><span style="color: #000000"> runat</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #000000"><</span><span style="color: #000000">div</span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #000000"><</span><span style="color: #000000">asp:FileUpload ID</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">FileUpload1</span><span style="color: #800000">"</span><span style="color: #000000"> runat</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">/></span><span style="color: #000000"> </span><span style="color: #000000"><</span><span style="color: #000000">asp:Button ID</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">Button1</span><span style="color: #800000">"</span><span style="color: #000000"> runat</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">server</span><span style="color: #800000">"</span><span style="color: #000000"> Text</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">UP</span><span style="color: #800000">"</span><span style="color: #000000"> OnClick</span><span style="color: #000000">=</span><span style="color: #800000">"</span><span style="color: #800000">Button1_Click</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">/></span><span style="color: #000000"> </span><span style="color: #000000"></</span><span style="color: #000000">div</span><span style="color: #000000">></span><span style="color: #000000"></span><span style="color: #000000"></</span><span style="color: #000000">form</span><span style="color: #000000">></span></div></pre> </div> 后台: <div class="cnblogs_code"> <pre><div><span style="color: #0000ff"> protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> Button1_Click(</span><span style="color: #0000ff">object</span><span style="color: #000000"> sender, EventArgs e) { UpImageFile(FileUpload1); } </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> UpImageFile(FileUpload fileLoad) { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (fileLoad.HasFile) { </span><span style="color: #0000ff">string</span><span style="color: #000000"> fileType </span><span style="color: #000000">=</span><span style="color: #000000"> fileLoad.PostedFile.ContentType; </span><span style="color: #0000ff">if</span><span style="color: #000000"> (fileType </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">image/bmp</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> fileType </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">image/pjpeg</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> fileType </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">image/gif</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> fileType </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">image/png</span><span style="color: #800000">"</span><span style="color: #000000">) { </span><span style="color: #0000ff">string</span><span style="color: #000000"> loadPath </span><span style="color: #000000">=</span><span style="color: #000000"> fileLoad.PostedFile.FileName; </span><span style="color: #008000">//</span><span style="color: #008000">等待上传文件的本地路径</span><span style="color: #008000"></span><span style="color: #000000"> System.Drawing.Image img </span><span style="color: #000000">=</span><span style="color: #000000"> System.Drawing.Image.FromFile(loadPath); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (img.Height </span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> img.Width </span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000">) { FileInfo info </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> FileInfo(loadPath); </span><span style="color: #0000ff">string</span><span style="color: #000000"> fName </span><span style="color: #000000">=</span><span style="color: #000000"> info.Name; </span><span style="color: #008000">//</span><span style="color: #008000">获取原文件名</span><span style="color: #008000"></span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> fileName </span><span style="color: #000000">=</span><span style="color: #000000"> DateTime.Now.ToString(</span><span style="color: #800000">"</span><span style="color: #800000">yyMMddhhmmss</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> fName; </span><span style="color: #008000">//</span><span style="color: #008000">在文件名中加入时间</span><span style="color: #008000"></span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> imgPath </span><span style="color: #000000">=</span><span style="color: #000000"> Server.MapPath(</span><span style="color: #800000">"</span><span style="color: #800000">/UpFile/OrImages/</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> fileName; </span><span style="color: #008000">//</span><span style="color: #008000">原文件路径</span><span style="color: #008000"></span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> thPath </span><span style="color: #000000">=</span><span style="color: #000000"> Server.MapPath(</span><span style="color: #800000">"</span><span style="color: #800000">/UpFile/ThImages/</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> fileName; </span><span style="color: #008000">//</span><span style="color: #008000">缩略图路径</span><span style="color: #008000"></span><span style="color: #000000"> fileLoad.SaveAs(imgPath); </span><span style="color: #008000">//</span><span style="color: #008000">保存原图片</span><span style="color: #008000"></span><span style="color: #000000"> MakeThumnail(imgPath, thPath); </span><span style="color: #008000">//</span><span style="color: #008000">生成缩略图</span><span style="color: #008000"></span><span style="color: #000000"> } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { </span><span style="color: #008000">//</span><span style="color: #008000">图片尺寸太小</span><span style="color: #008000"></span><span style="color: #000000"> } } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { </span><span style="color: #008000">//</span><span style="color: #008000">文件格式不对</span><span style="color: #008000"></span><span style="color: #000000"> } } } </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> MakeThumnail(</span><span style="color: #0000ff">string</span><span style="color: #000000"> orPath, </span><span style="color: #0000ff">string</span><span style="color: #000000"> thPath) { System.Drawing.Image img </span><span style="color: #000000">=</span><span style="color: #000000"> System.Drawing.Image.FromFile(orPath); </span><span style="color: #0000ff">int</span><span style="color: #000000"> width </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">100</span><span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000">设置缩略图的宽为100</span><span style="color: #008000"></span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> height </span><span style="color: #000000">=</span><span style="color: #000000"> img.Height </span><span style="color: #000000">*</span><span style="color: #000000"> width </span><span style="color: #000000">/</span><span style="color: #000000"> img.Width; </span><span style="color: #008000">//</span><span style="color: #008000">缩略图的高按比例缩小</span><span style="color: #008000"></span><span style="color: #000000"> System.Drawing.Image bitmap </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> System.Drawing.Bitmap(width, height); </span><span style="color: #008000">//</span><span style="color: #008000">创建一个空位图</span><span style="color: #008000"></span><span style="color: #000000"> System.Drawing.Graphics g </span><span style="color: #000000">=</span><span style="color: #000000"> System.Drawing.Graphics.FromImage(bitmap); </span><span style="color: #008000">//</span><span style="color: #008000">创建画板</span><span style="color: #008000"></span><span style="color: #000000"> g.InterpolationMode </span><span style="color: #000000">=</span><span style="color: #000000"> System.Drawing.Drawing2D.InterpolationMode.High; </span><span style="color: #008000">//</span><span style="color: #008000">设定为高质量插值</span><span style="color: #008000"></span><span style="color: #000000"> g.SmoothingMode </span><span style="color: #000000">=</span><span style="color: #000000"> System.Drawing.Drawing2D.SmoothingMode.HighQuality; </span><span style="color: #008000">//</span><span style="color: #008000">指定高质量低速度呈现</span><span style="color: #008000"></span><span style="color: #000000"> g.Clear(Color.Transparent); g.DrawImage(img, </span><span style="color: #0000ff">new</span><span style="color: #000000"> Rectangle(</span><span style="color: #800080">0</span><span style="color: #000000">, </span><span style="color: #800080">0</span><span style="color: #000000">, width, height)); </span><span style="color: #0000ff">try</span><span style="color: #000000"> { bitmap.Save(thPath, System.Drawing.Imaging.ImageFormat.Jpeg); </span><span style="color: #008000">//</span><span style="color: #008000">以JPG格式保存图片</span><span style="color: #008000"></span><span style="color: #000000"> } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (System.Exception e) { </span><span style="color: #0000ff">throw</span><span style="color: #000000"> e; } </span><span style="color: #0000ff">finally</span><span style="color: #000000"> { img.Dispose(); bitmap.Dispose(); g.Dispose(); } }</span></div></pre> </div> </div> </div>
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3