扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月12日
关键字: 编程
ÔÚ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£¬ÒÔ±ãÓÚ¹ÜÀí¡£
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者