首页学院源码字体下载软件电脑书| 素材图片模版壁纸酷站图标笔刷纹理|小游戏博客QQ表情投票计数器JoyRssMail
动态网站制作指南
首页|网络编程|站长之家|网页制作|图形图象|操作系统|冲浪宝典|软件教学|网络办公|手机学院|邮件系统|网络安全|认证考试
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket

在ASP.NET中上传图片并生成缩略图的C#源码


发表日期:2005-1-22 |


在ASP.NET中上传图片并生成缩略图的C#源码 <FONT size=4><FONT size=4><FONT size=4>using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;

namespace eMeng.Exam
{
/// <summary>
/// Thumbnail 的摘要说明。
/// </summary>
public class Thumbnail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Label1.Text = "<h3>在ASP.NET里轻松实炙趼酝?lt;/h3>";
Button1.Text = "上载并显示缩略图";

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
HttpPostedFile MyPostedFile = MyFileColl[0];

if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
{
Response.Write("无效的图形格式。");
return;
}
GetThumbNail(MyPostedFile.FileName, 100, 100,
MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream);
}
private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
{
if ((strContentType.ToString().ToLower()) == "image/pjpeg")
{
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
else if ((strContentType.ToString().ToLower()) == "image/gif")
{
return System.Drawing.Imaging.ImageFormat.Gif;
}
else if ((strContentType.ToString().ToLower()) == "image/bmp")
{
return System.Drawing.Imaging.ImageFormat.Bmp;
}
else if ((strContentType.ToString().ToLower()) == "image/tiff")
{
return System.Drawing.Imaging.ImageFormat.Tiff;
}
else if ((strContentType.ToString().ToLower()) == "image/x-icon")
{
return System.Drawing.Imaging.ImageFormat.Icon;
}
else if ((strContentType.ToString().ToLower()) == "image/x-png")
{
return System.Drawing.Imaging.ImageFormat.Png;
}
else if ((strContentType.ToString().ToLower()) == "image/x-emf")
{
return System.Drawing.Imaging.ImageFormat.Emf;
}
else if ((strContentType.ToString().ToLower()) == "image/x-exif")
{
return System.Drawing.Imaging.ImageFormat.Exif;
}
else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
{
return System.Drawing.Imaging.ImageFormat.Wmf;
}
else
{
return System.Drawing.Imaging.ImageFormat.MemoryBmp;
}
}

private void GetThumbNail(string strFileName, int iWidth, int iheight,
string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream)
{
System.Drawing.Image oImg;

if (blnGetFromFile)
{
oImg = System.Drawing.Image.FromFile(strFileName);
}
else
{
oImg = System.Drawing.Image.FromStream(ImgStream);
}
oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);
string strGuid = System.Guid.NewGuid().ToString().ToUpper();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
Response.ContentType = strContentType;
MemoryStream MemStream = new MemoryStream();
oImg.Save(MemStream, GetImageType(strContentType));
MemStream.WriteTo(Response.OutputStream);
}

}
}
</FONT></FONT></FONT><FONT color=#ff0000 size=4></FONT><FONT size=4>功能:
1。把图片文件(JPG GIF PNG)上传,
2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
3。并自动生成指定宽度的(在web.config中设置宽度)
4。和指定格式的(在web.config中指定缩略图的格式)
5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
6。可以判断是否已经存在文件
7。如果不覆盖,则给出错误
8。如果选中"覆盖原图"checkbox,则覆盖原图。
9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
11。缩略图的文件名尺寸。
12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。

--------------------
public void UploadFile(object sender, System.EventArgs e)
{

string imgNameOnly, imgNameNoExt, imgExt;
string imgThumbnail;
int erroNumber = 0;
System.Drawing.Image oriImg, newImg;
string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString();
string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower();
int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]);
string fileExt;

StringBuilder picInfo = new StringBuilder();

if(Page.IsValid)
{


for(int i = 0;i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();

imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
if(fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
{

if(System.IO.File.Exists(strFePicSavePath + imgNameOnly) && (checkboxlistRewrite.Items[i].Selected == false))
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名<br/>" );
}
}
else
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br/>" );
}

}

if(erroNumber > 0)
{
picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br/>");
}
else
{
for(int i = 0;i < Request.Files.Count; i++)
{

HttpPostedFile PostedFile = Request.Files[i];
imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName);
imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();


oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
newImg = oriImg.GetThumbnailImage(intFeThumbWidth, intFeThumbWidth * oriImg.Height/oriImg.Width,null,new System.IntPtr(0));
switch(imgExt)
{
//case ".jpeg":
case ".jpg":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".gif":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".png":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Png);
break;
}

//oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

switch(strFePicThumbFormat)
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case "jpg":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;
case "gif":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif",System.Drawing.Imaging.ImageFormat.Gif);
imgThumbnail = imgNameOnly + "_thumb.gif";
break;
case "png":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png",System.Drawing.Imaging.ImageFormat.Png);
imgThumbnail = imgNameOnly + "_thumb.png";
break;
default:
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;

}//switch

picInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB<br/>");
picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br/><br/>");

oriImg.Dispose();
newImg.Dispose();

}//for
picInfo.Append("<font color=red>所有操作成功</font><br/>");

}// if erronumber = 0

 

}
else
{
picInfo.Append("<font color=red>有错误,请检查。操作未成功</font><br/>");

}

lblPicInfo.Text = picInfo.ToString();

}
</FONT>

关注此文的读者还看过:
·SharpWebMail介绍和安装
·一个简单的数据操作类
·ASP.net在页面所有内容生成后、输出内容前对页面内容进行操作
·asp.net中XML文件作为中间件进行数据交换功能的实现(c#,sql2000)
·ASP.Net防止刷新自动触发事件的解决方案
·在asp.net页面中使用异步读取
·一个读取扩展名为xml的资源文件的方法
·在ASP.NET中访问SQL 2005报表服务
·ASP.NET中水晶报表的使用
·解决 ASP.NET 中目录访问权限的问题

浏览全部ASP.NET的内容 PS笔刷下载 39种不同语言在线翻译
文章搜索服务





邮件订阅服务
输入你的邮件地址,你将不会错过任何关于<ASP.NET技巧>的内容
Powered by JoyRssMail

网络编程文章分类

本月文章推荐
. ASP.NET生成高质量缩略图通用函数(.
. 关闭按钮点击事件的捕捉 .
. 如何实现ASP.NET网站个性化?.
. ASP.NET2.0服务器控件之捕获回传事.
. asp.net 1.1/ 2.0 中快速实现单点登.
. asp.net StreamReader 创建文件.
. Asp.Net中NHiernate的Session的管理.
. 何时.NET中AppDomain会回收? .
. [先安装IIS后安装.NET]如何配置服务.
. 在虚拟主机上用asp.net轻松实现url.
. ASP.NET热点问题解答14个.
. DataGrid的多行提交.
. Access 通用数据访问类(asp.net 2..
. 解决ASP.NET的进程帐户没有访问 II.
. 配置.net 3.0开发环境.
. ASP.NET 2.0的异步页面刷新.
. 动态改变asp.net网页的标题.
. URL编码处理.
. 简单而又复杂的ASP.NET编程模型 .
. ASP.NET中前台javascript与后台代码.
. ASP.NET缓存策略经验谈.
. Asp.net中如何过滤html,js,css代码.
. ASP.NET中Datagrid常见错误.
. 跳出封装剖析ASP.NET脚本回调的原理.
. 在.net中创建使用全球唯一标识符.
. ASP.Net应用程序的多进程模型 .
. GridView 分页导航.
. lucene并行建索引解决方案.
. Asp.net(c#)实现多线程断点续传.
. ASP.NET程序中常用的三十三种代码.
  最新网站源码 最新软件下载
2009-7-3 MyBB v1.4.8
2009-7-3 miniBB v2.3
2009-7-3 Modoer多功能点评系统 v1.1.0 Bu
2009-7-3 Modoer多功能点评系统 v1.1.0 Bu
2009-7-3 Zoomla!逐浪CMS v3.2
2009-7-3 谷搜Linux邮件群发系统 v1.1
2009-7-3 nicEdit Version 编辑器 v0.9 r23
2009-7-3 捷扬文章系统 v0.7.3
2009-7-3 ITF4博客程序(24个风格)
2009-4-4 SharePoint Designer 2007
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
2009-3-20 Internet Explorer 8 简体中文正
  发表评论
姓 名: 验证码: [ 查看评论 ]
内 容:
 
站长工具:Google PR查询|网站速度测试|CSS在线编辑器|OPEN参数生成器|弹出式窗口代码产生器|密码登录生成器|在线按钮生成器|Meta标签生成器|邮箱图标在线生成|多色彩特效字代码生成器|网页代码调试器|在线FTP登陆|Flash取色器|配色代码对照表|配色辞典|CSS生成器|CSS在线压缩|广告代码|框架网页代码生成器|js/vbs加密|md5加密|进制转换|UTF-8 转换工具|在线调色板|Html转换js|Html转换asp|Html转换php|Html转换perl
实用工具:汉字翻译拼音|拼音字典|在线翻译|火星文|在线网速测试|符号对照表|个税计算|公积金贷款计算|购房银行按揭利率计算|实时汇率查询换算|经典小工具|汉字简繁转换|普通单位换算|公制单位换算|生辰老黄历|国内电话区号|国家代码与域名缩写|文字加密解密|元素周期表|健康查询|世界时间|全国各地车牌查询|全国车辆交通违章查询|万年历|二十四节气|汉字横竖排版|手机号码查询|计算器|ip搜索
广告刊登 | 版权声明 | 联系我们 | 加入收藏 | RSS订阅
Copyright © 2000-2009 www.knowsky.com All rights reserved | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 コーチ ホームページ作成
過払い請求 現金化 債務整理 現金化