在ASP.NET中上传图片并生成缩略图的C#源码(1)
点击次数:28 次 发布日期:2008-11-26 14:09:57 作者:源代码网
|
源代码网推荐 在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,以便于管理。 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
