HTML:
 <asp:Label ID="lblPicInfo" runat="server"></asp:Label> 
  <asp:Label ID="lblPicInfo" runat="server"></asp:Label> 
 <asp:Button ID="Button1" runat="server" Text="原始图片" />
        <asp:Button ID="Button1" runat="server" Text="原始图片" /> <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button2" runat="server" OnClick="UploadFile" Text="保存缩略图" /></div>
        <asp:Button ID="Button2" runat="server" OnClick="UploadFile" Text="保存缩略图" /></div>
CS代码:
 using System;
using System; using System.Collections;
using System.Collections;  using System.ComponentModel;
using System.ComponentModel;  using System.Data;
using System.Data;  using System.Drawing;
using System.Drawing;  using System.Web;
using System.Web;  using System.Web.SessionState;
using System.Web.SessionState;  using System.Web.UI;
using System.Web.UI;  using System.Web.UI.WebControls;
using System.Web.UI.WebControls;  using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;  using System.IO;
using System.IO;  using System.Drawing.Imaging;
using System.Drawing.Imaging; using System.Text;
using System.Text;  
 /// <summary>
    /// <summary>  /// Thumbnail 的摘要说明。
    /// Thumbnail 的摘要说明。  /// </summary>
    /// </summary>  public partial class Thumbnail : System.Web.UI.Page
    public partial class Thumbnail : System.Web.UI.Page {
    { 
      
 private void Page_Load(object sender, System.EventArgs e)
        private void Page_Load(object sender, System.EventArgs e) {
        { // 在此处放置用户代码以初始化页面
            // 在此处放置用户代码以初始化页面  
           Button1.Text = "上载并显示缩略图";
            Button1.Text = "上载并显示缩略图";
 }
        }
 Web 窗体设计器生成的代码
        Web 窗体设计器生成的代码
 private void Button1_Click(object sender, System.EventArgs e)
        private void Button1_Click(object sender, System.EventArgs e) {
        { HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
            HttpFileCollection MyFileColl = HttpContext.Current.Request.Files; HttpPostedFile MyPostedFile = MyFileColl[0];
            HttpPostedFile MyPostedFile = MyFileColl[0];
 if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
            if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0) {
            { Response.Write("无效的图形格式。");
                Response.Write("无效的图形格式。"); return;
                return; }
            } 
          GetThumbNail(MyPostedFile.FileName, 100, 100,
            GetThumbNail(MyPostedFile.FileName, 100, 100, MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream);
            MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream); }
        } private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
        private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType) {
        { if ((strContentType.ToString().ToLower()) == "image/pjpeg")
            if ((strContentType.ToString().ToLower()) == "image/pjpeg") {
            { return System.Drawing.Imaging.ImageFormat.Jpeg;
                return System.Drawing.Imaging.ImageFormat.Jpeg; }
            } else if ((strContentType.ToString().ToLower()) == "image/gif")
            else if ((strContentType.ToString().ToLower()) == "image/gif") {
            { return System.Drawing.Imaging.ImageFormat.Gif;
                return System.Drawing.Imaging.ImageFormat.Gif; }
            } else if ((strContentType.ToString().ToLower()) == "image/bmp")
            else if ((strContentType.ToString().ToLower()) == "image/bmp") {
            { return System.Drawing.Imaging.ImageFormat.Bmp;
                return System.Drawing.Imaging.ImageFormat.Bmp; }
            } else if ((strContentType.ToString().ToLower()) == "image/tiff")
            else if ((strContentType.ToString().ToLower()) == "image/tiff") {
            { return System.Drawing.Imaging.ImageFormat.Tiff;
                return System.Drawing.Imaging.ImageFormat.Tiff; }
            } else if ((strContentType.ToString().ToLower()) == "image/x-icon")
            else if ((strContentType.ToString().ToLower()) == "image/x-icon") {
            { return System.Drawing.Imaging.ImageFormat.Icon;
                return System.Drawing.Imaging.ImageFormat.Icon; }
            } else if ((strContentType.ToString().ToLower()) == "image/x-png")
            else if ((strContentType.ToString().ToLower()) == "image/x-png") {
            { return System.Drawing.Imaging.ImageFormat.Png;
                return System.Drawing.Imaging.ImageFormat.Png; }
            } else if ((strContentType.ToString().ToLower()) == "image/x-emf")
            else if ((strContentType.ToString().ToLower()) == "image/x-emf") {
            { return System.Drawing.Imaging.ImageFormat.Emf;
                return System.Drawing.Imaging.ImageFormat.Emf; }
            } else if ((strContentType.ToString().ToLower()) == "image/x-exif")
            else if ((strContentType.ToString().ToLower()) == "image/x-exif") {
            { return System.Drawing.Imaging.ImageFormat.Exif;
                return System.Drawing.Imaging.ImageFormat.Exif; }
            } else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
            else if ((strContentType.ToString().ToLower()) == "image/x-wmf") {
            { return System.Drawing.Imaging.ImageFormat.Wmf;
                return System.Drawing.Imaging.ImageFormat.Wmf; }
            } else
            else {
            { return System.Drawing.Imaging.ImageFormat.MemoryBmp;
                return System.Drawing.Imaging.ImageFormat.MemoryBmp; }
            } }
        }
 private void GetThumbNail(string strFileName, int iWidth, int iheight,
        private void GetThumbNail(string strFileName, int iWidth, int iheight, string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream)
        string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream) {
        { System.Drawing.Image oImg;
            System.Drawing.Image oImg;
 if (blnGetFromFile)
            if (blnGetFromFile) {
            { oImg = System.Drawing.Image.FromFile(strFileName);
                oImg = System.Drawing.Image.FromFile(strFileName); }
            } 
                  else
            else {
            { oImg = System.Drawing.Image.FromStream(ImgStream);
                oImg = System.Drawing.Image.FromStream(ImgStream); }
            } //oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);
            //oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero); oImg = oImg.GetThumbnailImage(oImg.Size.Width, oImg.Size.Height, null, IntPtr.Zero);
            oImg = oImg.GetThumbnailImage(oImg.Size.Width, oImg.Size.Height, null, IntPtr.Zero); string strGuid = System.Guid.NewGuid().ToString().ToUpper();
            string strGuid = System.Guid.NewGuid().ToString().ToUpper(); string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
            string strFileExt = strFileName.Substring(strFileName.LastIndexOf(".")); Response.ContentType = strContentType;
            Response.ContentType = strContentType; MemoryStream MemStream = new MemoryStream();
            MemoryStream MemStream = new MemoryStream(); oImg.Save(MemStream, GetImageType(strContentType));
            oImg.Save(MemStream, GetImageType(strContentType)); MemStream.WriteTo(Response.OutputStream);
            MemStream.WriteTo(Response.OutputStream); }
        }


 public void UploadFile(object sender, System.EventArgs e)
        public void UploadFile(object sender, System.EventArgs e) {
        { HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
            HttpFileCollection MyFileColl = HttpContext.Current.Request.Files; HttpPostedFile MyPostedFile = MyFileColl[0];
            HttpPostedFile MyPostedFile = MyFileColl[0]; if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
            if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0) {
            { Response.Write("无效的图形格式。");
                Response.Write("无效的图形格式。"); return;
                return; }
            } string imgNameOnly, imgNameNoExt, imgExt;
            string imgNameOnly, imgNameNoExt, imgExt; string imgThumbnail;
            string imgThumbnail; int erroNumber = 0;
            int erroNumber = 0; System.Drawing.Image oriImg, newImg;
            System.Drawing.Image oriImg, newImg; string strFePicSavePath = Server.MapPath("SmallPic")+"\\";
            string strFePicSavePath = Server.MapPath("SmallPic")+"\\"; string strFePicThumbFormat = ".jpg";
            string strFePicThumbFormat = ".jpg"; int intFeThumbWidth = 30;
            int intFeThumbWidth = 30; // string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString();
           // string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString(); // string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower();
           // string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower(); //int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]);
            //int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]); string fileExt;
            string fileExt;
 StringBuilder picInfo = new StringBuilder();
            StringBuilder picInfo = new StringBuilder();
 if (Page.IsValid)
            if (Page.IsValid) {
            {

 for (int i = 0; i < Request.Files.Count; i++)
                for (int i = 0; i < Request.Files.Count; i++) {
                { HttpPostedFile PostedFile = Request.Files[i];
                    HttpPostedFile PostedFile = Request.Files[i]; fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();
                    fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();
 imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
                    imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName); if (fileExt != ".jpg" && fileExt != ".gif" && fileExt == ".png")
                    if (fileExt != ".jpg" && fileExt != ".gif" && fileExt == ".png") {
                    { erroNumber = erroNumber + 1;
                        erroNumber = erroNumber + 1; picInfo.Append("<b>错误:</b>文件(" + (i + 1) + ") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br/>");
                        picInfo.Append("<b>错误:</b>文件(" + (i + 1) + ") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br/>");
 
                        }
                    } 
                   
 }
                }
 if (erroNumber > 0)
                if (erroNumber > 0) {
                { picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br/>");
                    picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br/>"); }
                } else
                else {
                { for (int i = 0; i < Request.Files.Count; i++)
                    for (int i = 0; i < Request.Files.Count; i++) {
                    {
 HttpPostedFile PostedFile = Request.Files[i];
                        HttpPostedFile PostedFile = Request.Files[i]; imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
                        imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName); imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName);
                        imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName); imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();
                        imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();

 oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
                        oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream); newImg = oriImg.GetThumbnailImage(intFeThumbWidth, intFeThumbWidth * oriImg.Height / oriImg.Width, null, new System.IntPtr(0));
                        newImg = oriImg.GetThumbnailImage(intFeThumbWidth, intFeThumbWidth * oriImg.Height / oriImg.Width, null, new System.IntPtr(0)); switch (imgExt)
                        switch (imgExt) {
                        { //case ".jpeg":
                            //case ".jpeg":  case ".jpg":
                            case ".jpg": oriImg.Save(strFePicSavePath + imgNameOnly, System.Drawing.Imaging.ImageFormat.Jpeg);
                                oriImg.Save(strFePicSavePath + imgNameOnly, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                                break; case ".gif":
                            case ".gif": oriImg.Save(strFePicSavePath + imgNameOnly, System.Drawing.Imaging.ImageFormat.Gif);
                                oriImg.Save(strFePicSavePath + imgNameOnly, System.Drawing.Imaging.ImageFormat.Gif); break;
                                break; case ".png":
                            case ".png": oriImg.Save(strFePicSavePath + imgNameOnly, System.Drawing.Imaging.ImageFormat.Png);
                                oriImg.Save(strFePicSavePath + imgNameOnly, System.Drawing.Imaging.ImageFormat.Png); break;
                                break; }
                        }
 //oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                        //oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
 switch (strFePicThumbFormat)
                        switch (strFePicThumbFormat) {
                        { //jpeg format can get the smallest file size, and the png is the largest size
                            //jpeg format can get the smallest file size, and the png is the largest size  //case "jpeg":
                            //case "jpeg":  case "jpg":
                            case "jpg": newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                                newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); imgThumbnail = imgNameOnly + "_thumb.jpg";
                                imgThumbnail = imgNameOnly + "_thumb.jpg"; break;
                                break; case "gif":
                            case "gif": newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif", System.Drawing.Imaging.ImageFormat.Gif);
                                newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif", System.Drawing.Imaging.ImageFormat.Gif); imgThumbnail = imgNameOnly + "_thumb.gif";
                                imgThumbnail = imgNameOnly + "_thumb.gif"; break;
                                break; case "png":
                            case "png": newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png", System.Drawing.Imaging.ImageFormat.Png);
                                newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png", System.Drawing.Imaging.ImageFormat.Png); imgThumbnail = imgNameOnly + "_thumb.png";
                                imgThumbnail = imgNameOnly + "_thumb.png"; break;
                                break; default:
                            default: newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                                newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); imgThumbnail = imgNameOnly + "_thumb.jpg";
                                imgThumbnail = imgNameOnly + "_thumb.jpg"; break;
                                break;
 }//switch
                        }//switch 
 picInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength / 1024 + "KB<br/>");
                        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/>");
                        picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br/><br/>");
 oriImg.Dispose();
                        oriImg.Dispose(); newImg.Dispose();
                        newImg.Dispose();
 }//for
                    }//for  picInfo.Append("<font color=red>所有操作成功</font><br/>");
                    picInfo.Append("<font color=red>所有操作成功</font><br/>");
 }// if erronumber = 0
                }// if erronumber = 0 


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

 
 
 
                    
                     
                    
                 
                    
                 
 
    
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号