您的位置:首页 > 编程语言 > ASP

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

2008-11-28 17:51 453 查看
<SCRIPT type=text/javascript><!--
google_ad_client = "pub-4490194096475053";
/* 内容页,300x250,第一屏 */
google_ad_slot = "3685991503";
google_ad_width = 300;
google_ad_height = 250;
//-->
</SCRIPT>

<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript>
</SCRIPT>

<SCRIPT>window.google_render_ad();</SCRIPT>
<IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4490194096475053&dt=1227865856789&lmt=1226285577&prev_slotnames=1891601125&output=html&slotname=3685991503&correlator=1227865856742&url=http%3A%2F%2Fwww.corange.cn%2Farchives%2F2008%2F09%2F1659.html&ea=0&ref=http%3A%2F%2Fwww.corange.cn%2Fhtml%2Fcorange__92.html&frm=0&ga_vid=2091876339.1227189135&ga_sid=1227865643&ga_hid=642045074&ga_fc=true&flash=9.0.124.0&u_h=768&u_w=1024&u_ah=715&u_aw=1024&u_cd=32&u_tz=480&u_java=true&dtd=31" frameBorder=0 width=300 scrolling=no height=250 allowTransparency></IFRAME>在ASP.NET中上传图片并生成缩略图的C#源码<FONTsize=4><FONTsize=4><FONTsize=4>usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
usingSystem.Drawing.Imaging;

namespaceeMeng.Exam
{
///<summary>
///Thumbnail的摘要说明。
///</summary>
publicclassThumbnail:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.LabelLabel1;
protectedSystem.Web.UI.WebControls.ButtonButton1;

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

}

#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

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

}
#endregion

privatevoidButton1_Click(objectsender,System.EventArgse)
{
HttpFileCollectionMyFileColl=HttpContext.Current.Request.Files;
HttpPostedFileMyPostedFile=MyFileColl[0];

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

privatevoidGetThumbNail(stringstrFileName,intiWidth,intiheight,
stringstrContentType,boolblnGetFromFile,System.IO.StreamImgStream)
{
System.Drawing.ImageoImg;

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

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

--------------------
publicvoidUploadFile(objectsender,System.EventArgse)
{

stringimgNameOnly,imgNameNoExt,imgExt;
stringimgThumbnail;
interroNumber=0;
System.Drawing.ImageoriImg,newImg;
stringstrFePicSavePath=ConfigurationSettings.AppSettings["FePicSavePath"].ToString();
stringstrFePicThumbFormat=ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower();
intintFeThumbWidth=Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]);
stringfileExt;

StringBuilderpicInfo=newStringBuilder();

if(Page.IsValid)
{

for(inti=0;i<Request.Files.Count;i++)
{
HttpPostedFilePostedFile=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("<fontcolor=red>全部操作均未完成,请修改错误,再进行操作</font><br/>");
}
else
{
for(inti=0;i<Request.Files.Count;i++)
{

HttpPostedFilePostedFile=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,newSystem.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)
{
//jpegformatcangetthesmallestfilesize,andthepngisthelargestsize
//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("<fontcolor=red>所有操作成功</font><br/>");

}//iferronumber=0

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

}

lblPicInfo.Text=picInfo.ToString();

}
</FONT>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息