/************************************************************************************
* Copyright (C) 2007 supesoft.com,All Rights Reserved *
* File: *
* FileUpLoadCommon.cs *
* Description: *
* 通用上传文件类 *
* Author: *
* Lzppcc *
* Lzppcc@hotmail.com *
* http://www.supesoft.com *
* Finish DateTime: *
* 2007年8月6日 *
* History: *
***********************************************************************************/
-
usingSystem;
-
usingSystem.Collections.Generic;
-
usingSystem.Text;
-
usingSystem.Web.UI.WebControls;
-
usingSystem.IO;
-
usingSystem.Web;
-
usingSystem.Drawing;
-
namespaceFrameWork
- {
-
-
-
-
publicclassFileUpLoadCommon
- {
-
#region"PrivateVariables"
-
privatestring_path;
-
privatestring_fileName;
-
privatestring_newFileName="";
-
privatestring_fileName_s;
-
privatestring_fileName_sy;
-
privatestring_fileName_syp;
-
privatestring_webFilePath;
-
privatestring_webFilePath_s;
-
privatestring_webFilePath_sy;
-
privatestring_webFilePath_syp;
-
privatestring_webFilePath_sypf;
-
privatestring_errorMsg;
-
privatebool_isDate=true;
-
privatebool_isThumbnail=true;
-
privatebool_isWatermarkT=false;
-
privatebool_isWatermarkP=false;
-
privatebool_isOnly=true;
-
-
privateint_width=Common.UpImgWidth;
-
privateint_height=Common.UpImgHeight;
-
privatestring_mode="Auto";
-
privatestring_addText="Moliyo.com";
-
privatestring_addPicture=Common.UpLoadDir+"shuiyin.jpg";
-
privateint_fileSize=0;
-
privateint_fileWidth=0;
-
privateint_fileHeight=0;
-
privatestring_fileType;
-
#endregion
-
- #region属性--旧文件名
-
-
-
-
publicstringfileName
- {
-
get
- {
-
return_fileName;
- }
- }
-
#endregion
-
- #region属性--上传后新文件名
-
-
-
-
publicstringnewFileName
- {
-
get
- {
-
return_newFileName;
- }
- }
-
#endregion
-
- #region属性--缩图文件名
-
-
-
-
publicstringfileName_s
- {
-
get
- {
-
return_fileName_s;
- }
- }
-
#endregion
-
- #region属性--加文字水印的图片
-
-
-
-
publicstringfileName_sy
- {
-
get
- {
-
return_fileName_sy;
- }
- }
-
#endregion
-
- #region属性--加图片水印的图片
-
-
-
-
publicstringfileName_syp
- {
-
get
- {
-
return_fileName_syp;
- }
- }
-
#endregion
-
- #region属性--上传的文件的路径
-
-
-
-
publicstringpath
- {
-
get
- {
-
return_path;
- }
- }
-
#endregion
-
- #region属性--文件大小
-
-
-
-
publicintfileSize
- {
-
get
- {
-
return_fileSize;
- }
- }
-
#endregion
-
- #region属性--图片原始的宽度
-
-
-
-
publicintfileWidth
- {
-
get
- {
-
return_fileWidth;
- }
- }
-
#endregion
-
- #region属性--图片原始的高度
-
-
-
-
publicintfileHeight
- {
-
get{return_fileHeight;}
- }
-
#endregion
-
- #region属性--文件的类型
-
-
-
-
publicstringfileType
- {
-
get{return_fileType;}
- }
-
#endregion
-
- #region属性--设置水印文字
-
-
-
-
publicstringaddText
- {
-
set{_addText=value;}
- }
-
#endregion
-
- #region属性--设置水印图片
-
-
-
-
publicstringaddPicture
- {
-
set{_addPicture=value;}
- }
-
#endregion
-
-
#region构造函数
-
-
-
-
publicFileUpLoadCommon()
-
:this("/public/",true)
- {
- }
-
-
-
-
-
-
publicFileUpLoadCommon(stringfilePath,boolisDate)
- {
- _path=filePath;
- _isDate=isDate;
-
if(_isDate)
-
_path+=DateTime.Now.ToString("yyyyMMdd")+"/";
-
stringp=HttpContext.Current.Server.MapPath(_path);
-
-
if(!Directory.Exists(p))
- Directory.CreateDirectory(p);
- }
-
#endregion
-
- #region方法--保存文件
-
-
-
-
-
-
-
-
publicboolSaveImage(FileUploadfu,intWidth,intHeight)
- {
- _width=Width;
- _height=Height;
-
returnSaveFile(fu,true);
- }
-
-
-
-
-
-
-
-
-
publicboolSaveImage(FileUploadfu,intWidth,intHeight,stringMode)
- {
- _width=Width;
- _height=Height;
- _mode=Mode;
-
returnSaveFile(fu,true);
- }
-
#endregion
-
- #region方法--保存文件
-
-
-
-
-
-
publicboolSaveFile(FileUploadfu,boolIsImage)
- {
-
if(fu.HasFile)
- {
-
stringfileContentType=fu.PostedFile.ContentType;
-
stringname=fu.PostedFile.FileName;
-
FileInfofile=newFileInfo(name);
- _fileType=fu.PostedFile.ContentType;
- _fileSize=fu.PostedFile.ContentLength;
-
boolisfileTypeImages=false;
-
if(fileContentType=="image/x-png"||fileContentType=="image/bmp"||fileContentType=="image/gif"||fileContentType=="image/pjpeg")
- {
-
isfileTypeImages=true;
- }
-
if(IsImage==true&&isfileTypeImages==false)
- {
-
_errorMsg="文件类型不是图片!";
- fu.Dispose();
-
returnfalse;
- }
-
-
if(!Common.Check_Char_Is(file.Extension.Substring(1).ToLower(),FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadFileExt.ToLower()))
- {
-
_errorMsg=string.Format("文件扩展名不符合系统需求:{0}",FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadFileExt);
- fu.Dispose();
-
returnfalse;
- }
-
if(_fileSize/1024>FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadSizeKb)
- {
-
_errorMsg=string.Format("上传文件超过系统允许大小:{0}K",FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadSizeKb);
- fu.Dispose();
-
returnfalse;
- }
-
_fileName=file.Name;
- _newFileName=CreateFileName()+file.Extension;
-
_webFilePath=HttpContext.Current.Server.MapPath(_path+_newFileName);
-
if(isfileTypeImages)
- {
-
_fileName_s="s_"+_newFileName;
-
if(_isOnly)
- {
-
_fileName_sy=_newFileName;
-
_fileName_syp=_newFileName;
- }
-
else
- {
-
_fileName_sy="sy_"+_newFileName;
-
_fileName_syp="syp_"+_newFileName;
- }
-
_webFilePath_s=HttpContext.Current.Server.MapPath(_path+_fileName_s);
-
_webFilePath_sy=HttpContext.Current.Server.MapPath(_path+_fileName_sy);
-
_webFilePath_syp=HttpContext.Current.Server.MapPath(_path+_fileName_syp);
-
_webFilePath_sypf=HttpContext.Current.Server.MapPath(_addPicture);
-
-
if(!File.Exists(_webFilePath))
- {
-
try
- {
-
fu.SaveAs(_webFilePath);
-
if(_isWatermarkT)
- AddShuiYinWord(_webFilePath,_webFilePath_sy);
-
if(_isWatermarkP)
- AddShuiYinPic(_webFilePath,_webFilePath_syp,_webFilePath_sypf);
-
if(_isThumbnail)
-
MakeThumbnail(_webFilePath,_webFilePath_s,_width,_height,_mode);
-
-
if(File.Exists(_webFilePath))
- {
- System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(_webFilePath);
-
try
- {
- _fileHeight=originalImage.Height;
- _fileWidth=originalImage.Width;
- }
-
finally
- {
- originalImage.Dispose();
- }
- }
-
_errorMsg=string.Format("提示:文件“{0}”成功上传,并生成“{1}”缩略图,文件类型为:{2},文件大小为:{3}B",_newFileName,_fileName_s,fu.PostedFile.ContentType,fu.PostedFile.ContentLength);
- fu.Dispose();
-
returntrue;
- }
-
catch(Exceptionex)
- {
-
_errorMsg="提示:文件上传失败,失败原因:"+ex.Message;
- }
- }
-
else
- {
-
_errorMsg="提示:文件已经存在,请重命名后上传";
- }
- }
-
else
- {
-
-
-
if(!File.Exists(_webFilePath))
- {
-
try
- {
-
fu.SaveAs(_webFilePath);
-
_errorMsg=string.Format("提示:文件“{0}”成功上传,并生成“{1}”缩略图,文件类型为:{2},文件大小为:{3}B",_newFileName,_fileName_s,fu.PostedFile.ContentType,fu.PostedFile.ContentLength);
- fu.Dispose();
-
returntrue;
- }
-
catch(Exceptionex)
- {
-
_errorMsg="提示:文件上传失败,失败原因:"+ex.Message;
- }
- }
-
else
-
_errorMsg="提示:文件已经存在,请重命名后上传";
- }
- }
- fu.Dispose();
-
returnfalse;
- }
-
#endregion
-
- #region方法--创建新的文件名
-
-
-
-
-
publicstringCreateFileName()
- {
-
stringguid=System.Guid.NewGuid().ToString().ToLower();
-
guid=guid.Replace("-","");
-
returnDateTime.Now.ToString("yyyyMMddhhmmss")+guid.Substring(0,4);
- }
-
#endregion
-
- #region方法--删除文件
-
-
-
-
-
publicstaticvoidDeleteFile(stringfilename)
- {
-
strings=HttpContext.Current.Server.MapPath(filename);
-
if(File.Exists(s))
- {
-
try
- {
- File.Delete(s);
- }
-
catch
- {}
- }
- }
-
#endregion
-
- #region方法--生成缩略图
-
-
-
-
-
-
-
-
-
-
publicstaticvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode)
- {
- System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);
-
inttowidth=width;
-
inttoheight=height;
-
intx=0;
-
inty=0;
-
intow=originalImage.Width;
-
intoh=originalImage.Height;
-
if(ow<towidth&&oh<toheight)
- {
- originalImage.Save(thumbnailPath);
- }
-
else
- {
-
switch(mode.ToUpper())
- {
-
case"HW":
-
break;
-
case"W":
- toheight=originalImage.Height*width/originalImage.Width;
-
break;
-
case"H":
- towidth=originalImage.Width*height/originalImage.Height;
-
break;
-
case"CUT":
-
if((double)originalImage.Width/(double)originalImage.Height>(double)towidth/(double)toheight)
- {
- oh=originalImage.Height;
- ow=originalImage.Height*towidth/toheight;
- y=0;
- x=(originalImage.Width-ow)/2;
- }
-
else
- {
- ow=originalImage.Width;
- oh=originalImage.Width*height/towidth;
- x=0;
- y=(originalImage.Height-oh)/2;
- }
-
break;
-
case"AUTO":
-
if(ow>oh)
- {
-
-
toheight=(int)((double)oh/(double)ow*(double)towidth);
- }
-
else
- {
-
-
towidth=(int)((double)ow/(double)oh*(double)toheight);
- }
-
break;
-
default:
-
break;
- }
-
-
Bitmapimg=newBitmap(towidth,toheight);
- img.SetResolution(72f,72f);
- Graphicsgdiobj=Graphics.FromImage(img);
- gdiobj.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- gdiobj.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- gdiobj.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- gdiobj.PixelOffsetMode=System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
-
gdiobj.FillRectangle(newSolidBrush(Color.White),0,0,
- towidth,toheight);
-
Rectangledestrect=newRectangle(0,0,
- towidth,toheight);
- gdiobj.DrawImage(originalImage,destrect,0,0,ow,
- oh,GraphicsUnit.Pixel);
-
System.Drawing.Imaging.EncoderParametersep=newSystem.Drawing.Imaging.EncoderParameters(1);
-
ep.Param[0]=newSystem.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,(long)100);
-
System.Drawing.Imaging.ImageCodecInfoici=GetEncoderInfo("image/jpeg");
-
try
- {
-
if(ici!=null)
- {
- img.Save(thumbnailPath,ici,ep);
- }
-
else
- {
- img.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- }
-
catch(System.Exceptione)
- {
-
throwe;
- }
-
finally
- {
- gdiobj.Dispose();
- img.Dispose();
- }
- }
- originalImage.Dispose();
- }
-
privatestaticSystem.Drawing.Imaging.ImageCodecInfoGetEncoderInfo(StringmimeType)
- {
-
intj;
- System.Drawing.Imaging.ImageCodecInfo[]encoders;
- encoders=System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
-
for(j=0;j<encoders.Length;++j)
- {
-
if(encoders[j].MimeType==mimeType)
-
returnencoders[j];
- }
-
returnnull;
- }
-
- #endregion
-
-
#region方法--在图片上增加文字水印
-
-
-
-
-
-
-
protectedvoidAddShuiYinWord(stringPath,stringPath_sy)
- {
- System.Drawing.Imageimage=System.Drawing.Image.FromFile(Path);
- System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(image);
- g.DrawImage(image,0,0,image.Width,image.Height);
-
System.Drawing.Fontf=newSystem.Drawing.Font("Verdana",16);
-
System.Drawing.Brushb=newSystem.Drawing.SolidBrush(System.Drawing.Color.Blue);
- g.DrawString(_addText,f,b,15,15);
- g.Dispose();
- image.Save(Path_sy);
- image.Dispose();
- }
-
#endregion
-
- #region方法--在图片上生成图片水印
-
-
-
-
-
-
-
-
protectedvoidAddShuiYinPic(stringPath,stringPath_syp,stringPath_sypf)
- {
- System.Drawing.Imageimage=System.Drawing.Image.FromFile(Path);
- System.Drawing.ImagecopyImage=System.Drawing.Image.FromFile(Path_sypf);
- System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(image);
-
g.DrawImage(copyImage,newSystem.Drawing.Rectangle(image.Width-copyImage.Width,image.Height-copyImage.Height,copyImage.Width,copyImage.Height),0,0,copyImage.Width,copyImage.Height,System.Drawing.GraphicsUnit.Pixel);
- g.Dispose();
- image.Save(Path_syp);
- image.Dispose();
- }
-
#endregion
- }
- }
分享到:
相关推荐
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中使用...
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中使用...
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中使用...
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中...
ImageUpload 图片上传并进行缩略图处理 24.网络 NetHelper 25.文件操作类 DirFileHelper FileOperateHelper INIFile 26.序列化 Serialize 序列化帮助类,还有例子 SerializeHelper 序列化帮助类,Xml序列化,...
可以选择设置图片水印或文字水印,并可设置水印嵌入图片的位置,设置水印可以有效防止图片盗链,同时不易被修改和使用,更能表现出一个网站的特色风格,设置好水印后,添加商品上传图片时,会自动在图片上嵌入水印...
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中...
可以选择设置图片水印或文字水印,并可设置水印嵌入图片的位置,设置水印可以有效防止图片盗链,同时不易被修改和使用,更能表现出一个网站的特色风格,设置好水印后,添加商品上传图片时,会自动在图片上嵌入水印...
5、产品上传图片自动生成缩略图,加快产品列表访问速度; 6、上传产品图片自动添加文字水印,加大盗图难度; 7、详细介绍可传附件,方便供客户下载PDF,Doc,Xls,RAR,ZIP等文件; 8、保存远程图片功能,当复制...
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中...
书名:《PHP开发实战1200例(第I卷)》(清华大学出版社.潘凯华.刘中华) PDF格式扫描版,全书分为5篇15章,共899页。2011年1月出版。...实例278 缩略图艺术库 371 实例279 提取图像的EXIF信息 374 6.2 控制、显示...
书名:《PHP开发实战1200例(第I卷)》(清华大学出版社.潘凯华.刘中华) PDF格式扫描版,全书分为5篇15章,共899页。2011年1月出版。...实例278 缩略图艺术库 371 实例279 提取图像的EXIF信息 374 6.2 控制、显示...
可以选择设置图片水印或文字水印,并可设置水印嵌入图片的位置,设置水印可以有效防止图片盗链,同时不易被修改和使用,更能表现出一个网站的特色风格,设置好水印后,添加商品上传图片时,会自动在图片上嵌入水印...
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中...
6、图片缩略查看及编辑:支持当前目录中图片文件类windows缩略图查看,支持单击预览图片进行图片编辑,具体包括对图片的缩放、旋转、水印、裁剪等功能。 7、文件的密码上传:支持用户为某目录设置上传密码,访客可...
图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中...
10.图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助类(ImageHelper.cs) 11.输入法帮助,全角 转换为半角(ImeHelper.cs) 12.Winform提示框 的摘要说明。(MessageUtil.cs) 13.包含互操作方法调用的...
加入订单缩略图显示 修复支付宝后期支付错误 网趣网上购物系统是一套简单易用、功能强大、用户使用最为广泛的网上购物开店平台,凭借多年的网上购物系统研发经验,软件功能日趋强大与完善、依托庞大的用户使用...
若您的服务器支持jpeg组件,则全站均可使用缩略图和水印功能,提高网站访问速度;另外上传的图片支持“在线剪裁”,帮助您定制最合适的主题图片和个性图片。 5.可以控制网站通用的各板块的排序,显示与否,添加自己...
8、支持批量上传图片或文件,并可选择已上传的图片文件,避免重复上传; 9、支持附件上传 10、支持在文章内容的末尾自动添加上如转载声明之类的附加内容,增加外链; 11、设置关键字及链接,自动在文章内容中...