代码语言
.
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
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
NET图片上传类(支持任何大小的缩略图)
作者:
Dezai.CN
/ 发布于
2012/2/7
/
745
<div>using System;</div> <div>using System.Web;</div> <div>using System.Drawing;</div> <div>using System.Threading;</div> <div></div> <div>namespace Happy2006.Web.Utility</div> <div>{</div> <div>/// <summary></div> <div>/// FileUpload 的摘要说明。</div> <div>/// 支持缩略图的图片上传类,若不传图片则无缩略图支持</div> <div>/// path里可以用 / 表示分割,其他不支持 ;/ 会自动转换为 /</div> <div>/// </summary></div> <div>public class FileUpload</div> <div>{</div> <div> public static System.Web.UI.Page objPage;</div> <div> private string m_strDestPath="";</div> <div> private string m_strThumbDestPath="";</div> <div> private int m_nThumbWidth=0;</div> <div> private int m_nThumbHeight=0;</div> <div> private long m_nMaxSize=0;</div> <div> private string m_strPreFixThumb="";</div> <div> private static FileUpload m_objFileUpload;</div> <div> private static Object m_classLock=typeof(FileUpload);</div> <div> public static FileUpload getInstance(long nMaxFileSize,string strDestPath)</div> <div> {</div> <div> objPage= (System.Web.UI.Page)System.Web.HttpContext.Current.Handler ;</div> <div> if (null==objPage)</div> <div> {</div> <div> return null;</div> <div> } <div> lock(m_classLock)</div> <div> {</div> <div> if(null==m_objFileUpload)</div> <div> {</div> <div> m_objFileUpload= new FileUpload( nMaxFileSize, strDestPath);</div> <div> } <div> } <div> return m_objFileUpload;</div> <div> } <div> public static FileUpload getInstance(</div> <div> long nMaxFileSize,</div> <div> string strDestPath,</div> <div> string strThumbDestPath,</div> <div> int nThumbWidth,</div> <div> int nThumbHeight,</div> <div> string strPreFixThumb</div> <div> )</div> <div> {</div> <div> objPage= (System.Web.UI.Page)System.Web.HttpContext.Current.Handler ;</div> <div> if (null==objPage)</div> <div> {</div> <div> return null;</div> <div> } <div> lock(m_classLock)</div> <div> {</div> <div> if(null==m_objFileUpload)</div> <div> {</div> <div> m_objFileUpload=</div> <div> new FileUpload(</div> <div> nMaxFileSize,</div> <div> strDestPath,</div> <div> strThumbDestPath,</div> <div> nThumbWidth,</div> <div> nThumbHeight,</div> <div> strPreFixThumb</div> <div> );</div> <div> } <div> } <div> return m_objFileUpload;</div> <div> } <div> /// <summary></div> <div> /// 路径格式转换为正确格式</div> <div> /// </summary></div> <div> /// <param name="strPath"></param></div> <div> /// <returns></returns></div> <div> private string PathFormat(string strPath)</div> <div> {</div> <div> string strReturn=strPath;</div> <div> if (strReturn.Length<=0)</div> <div> {</div> <div> return "";</div> <div> } <div> strReturn=strReturn.Replace("//","/");</div> <div> if(strReturn.Substring(strReturn.Length-1)!= "/")</div> <div> {</div> <div> strReturn+="/";</div> <div> } <div> return strReturn;</div> <div> } <div> /// <summary></div> <div> ///</div> <div> /// </summary></div> <div> /// <param name="nMaxFileSize">限制最大文件大小</param></div> <div> /// <param name="strDestPath">文件上传目的路径</param></div> <div></div> <div> public FileUpload(long nMaxFileSize,string strDestPath)</div> <div> {</div> <div> m_strDestPath=PathFormat(strDestPath);</div> <div> m_nMaxSize=nMaxFileSize;</div> <div> } <div> /// <summary></div> <div> ///</div> <div> /// </summary></div> <div> /// <param name="nMaxFileSize">单位KB</param></div> <div> /// <param name="strDestPath"></param></div> <div> /// <param name="strThumbDestPath">缩略图路径,为空串时,不生成缩略图</param></div> <div> /// <param name="nThumbWidth"></param></div> <div> /// <param name="nThumbHeight"></param></div> <div> /// <param name="strPreFixThumb">缩略图前缀</param></div> <div> public FileUpload(long nMaxFileSize,</div> <div> string strDestPath,</div> <div> string strThumbDestPath,</div> <div> int nThumbWidth,</div> <div> int nThumbHeight,</div> <div> string strPreFixThumb)</div> <div> {</div> <div> m_strDestPath=PathFormat(strDestPath);</div> <div> m_nMaxSize=nMaxFileSize;</div> <div> m_strThumbDestPath=PathFormat(strThumbDestPath);</div> <div> m_nThumbHeight=nThumbHeight;</div> <div> m_nThumbWidth=nThumbWidth;</div> <div> m_strPreFixThumb=strPreFixThumb;</div> <div> } <div> public bool Upload(string strFileInputBoxID,</div> <div> string strDestFileName,</div> <div> out long nSrcFileSize,</div> <div> out string strSrcFileName,</div> <div> out string strSrcFileExt,</div> <div> out string strError)</div> <div> {</div> <div> nSrcFileSize=0;</div> <div> strSrcFileName="";</div> <div> strSrcFileExt="";</div> <div> strError="";</div> <div> System.Web.UI.HtmlControls.HtmlInputFile objFileCtrl=(System.Web.UI.HtmlControls.HtmlInputFile)objPage.FindControl(strFileInputBoxID);</div> <div> if (null==objFileCtrl)</div> <div> {</div> <div> strError="没有页对象PAGE";</div> <div> return false;</div> <div> } <div> if (objFileCtrl.PostedFile.ContentLength==0)</div> <div> {</div> <div> strError="上传文件内容为空";</div> <div> return false;</div> <div> } <div> if(m_nMaxSize<=nSrcFileSize && m_nMaxSize>0)</div> <div> {</div> <div> strError="上传文件太大:"+m_nMaxSize.ToString() +" < "+ nSrcFileSize.ToString();</div> <div> return false;</div> <div> } <div> nSrcFileSize=(objFileCtrl.PostedFile.ContentLength/1024);</div> <div> strSrcFileName=objFileCtrl.PostedFile.FileName;</div> <div> strSrcFileExt=strSrcFileName.Substring(strSrcFileName.LastIndexOf(".")+1);</div> <div> try</div> <div> {</div> <div> objFileCtrl.PostedFile.SaveAs( m_strDestPath + strDestFileName + "."+ strSrcFileExt);</div> <div> } <div> catch</div> <div> {</div> <div> strError="上传文件保存失败,请查看["+ m_strDestPath + "]路径是否可写";</div> <div> return false;</div> <div> } <div> int nSrcWidth,nSrcHeight;</div> <div> if (m_strThumbDestPath.Length>0)</div> <div> {</div> <div> if(false==GenerateThumb(m_strDestPath + strDestFileName + "."+ strSrcFileExt ,</div> <div> m_strThumbDestPath,</div> <div> m_strPreFixThumb+"_" + strDestFileName + "."+ strSrcFileExt ,</div> <div> m_nThumbWidth,</div> <div> m_nThumbHeight,</div> <div> out nSrcWidth,</div> <div> out nSrcHeight,</div> <div> out strError))</div> <div> {</div> <div> return false;</div> <div> } <div> } <div> return true;</div> <div> } <div> /// <summary></div> <div> ///</div> <div> /// </summary></div> <div> /// <param name="strSrcFilePathName">文件源</param></div> <div> /// <param name="strDestPath">缩略图路径</param></div> <div> /// <param name="strDestFileName">小图文件名包括扩展名</param></div> <div> /// <param name="nDestWidth">为0时以nDestHeight为标准</param></div> <div> /// <param name="nDestHeight">为0时以nDestWidth为标准</param></div> <div> /// <param name="strError">返回的错误信息</param></div> <div> /// <returns></returns></div> <div> private bool GenerateThumb( string strSrcFilePathName,</div> <div> string strDestPath,</div> <div> string strDestFileName,</div> <div> int nDestWidth,</div> <div> int nDestHeight,</div> <div> out int nSrcWidth,</div> <div> out int nSrcHeight,</div> <div> out string strError)</div> <div> {</div> <div> //'生成缩略图</div> <div> int nWidth=nDestWidth;</div> <div> int nHeight=nDestHeight;</div> <div> nSrcWidth=0;</div> <div> nSrcHeight=0;</div> <div> strError="";</div> <div> System.Drawing.Image objImg=System.Drawing.Image.FromFile(strSrcFilePathName);</div> <div> if (null==objImg)</div> <div> {</div> <div> strError="原图不存在";</div> <div> return false;</div> <div> } <div> nSrcWidth=objImg.Width;</div> <div> nSrcHeight=objImg.Height;</div> <div> if (0==nWidth)</div> <div> {</div> <div> nWidth=Convert.ToInt32((Convert.ToDouble(nSrcWidth)/Convert.ToDouble(nSrcHeight)*nHeight));</div> <div> } <div> if (0==nHeight)</div> <div> {</div> <div> nHeight=Convert.ToInt32((Convert.ToDouble(nSrcHeight)/Convert.ToDouble(nSrcWidth)*nWidth));</div> <div> } <div> System.Drawing.Image objImgReturn;</div> <div> try</div> <div> {</div> <div> System.Drawing.Image.GetThumbnailImageAbort objFunctionCall=null;</div> <div> objImgReturn</div> <div> =objImg.GetThumbnailImage(nWidth,nHeight,objFunctionCall,new System.IntPtr());</div> <div> objImgReturn.Save(strDestPath+strDestFileName);</div> <div> } <div> catch</div> <div> {</div> <div> strError="生成缩略图失败!确认路径可写["+ strDestPath+strDestFileName +"],预生成大小为 "+ nWidth.ToString() +" x "+ nHeight.ToString()+" "+ nSrcHeight.ToString() + " " ;</div> <div> return false;</div> <div> } <div> objImg.Dispose();</div> <div> objImgReturn.Dispose();</div> <div> return true;</div> <div> } <div>} <div>} <div></div> <div>代码调用:</div> <div> Happy2006.Web.Utility.FileUpload objFile;</div> <div> if(!chkThumb.Checked)//是否支持缩略图生成</div> <div> {</div> <div> objFile=Happy2006.Web.Utility.FileUpload.getInstance(0,</div> <div> Server.MapPath(@"/Happy2006WebTest/TestFile"));</div> <div> }else</div> <div> {</div> <div> objFile=Happy2006.Web.Utility.FileUpload.getInstance(0,</div> <div> Server.MapPath(@"/Happy2006WebTest/TestFile"),</div> <div> @"D:/Happy2006/TestFile",120,0,"thumb");</div> <div> } <div></div> <div> string strFileName;</div> <div> long nFileSize;</div> <div> string strError;</div> <div> string strFileExt;</div> <div> if (null!=objFile)</div> <div> {</div> <div> objFile.Upload("fileupload","xxxx",out nFileSize,out strFileName,out strFileExt,out strError);</div> <div> Response.Write(strError);</div> <div> } </div>
试试其它关键字
图片上传类
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3