cAng^Er

          不懂我的人 , 離不了我 , 該了解了解我 !而懂我的人 , 更離不了我 , 因為他們愛我 。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 16 文章 :: 18 評論 :: 0 Trackbacks

          參數說明
          FileBox:上傳表單中的表單名
          SavePath:保存路徑
          ThumbnailWidth:略縮圖寬
          ThumbnailHeight:略縮圖高
          FilePath:文件保存路徑(輸出參數)
          FileName:文件名稱(輸出參數)
          LastError:(輸出錯誤)
          public static bool UploadImages(System.Web.UI.HtmlControls.HtmlInputFile FileBox,string SavePath,Int32 ThumbnailWidth,Int32 ThumbnailHeight,out String FilePath,out String FileName,out string LastError)
          {
          DateTime datTime=System.DateTime.Now;
          String strTemp;
          string strFileName = FileBox.PostedFile.FileName;
          bool blnResult;
          LastError="";
          System.Drawing.Image xImage;
          System.Drawing.Bitmap xBitmap;
          int PhotoHeight,PhotoWidth;
          string BasePath=System.Web.HttpContext.Current.Server.MapPath("../");
          Rectangle NewPhoto;
          System.Drawing.Imaging.ImageFormat xObject;
          string Extension="";
          FileName="";
          FilePath="";

          try
          {
          if(strFileName=="")
          {
          LastError="請點擊瀏覽選擇要上傳的文件!";
          return false;
          }
          if(Convert.ToDecimal(FileBox.PostedFile.ContentLength)<= 2000000 &&Convert.ToDecimal(FileBox.PostedFile.ContentLength)>1500)
          {
          //查看擴展名是否合法
          Extension=Path.GetExtension(strFileName);
          Extension=Extension.ToLower();
          if(Extension==".gif" || Extension==".jpg" || Extension==".jpeg" || Extension==".bmp")
          {
          switch(Extension)
          {
          case ".gif":
          xObject=System.Drawing.Imaging.ImageFormat.Gif;
          break;
          case ".bmp":
          xObject=System.Drawing.Imaging.ImageFormat.Bmp;
          break;
          default:
          xObject=System.Drawing.Imaging.ImageFormat.Jpeg;

          break;
          }
          }
          else
          {
          LastError="上傳的文件不是有效的圖片文件,請上傳格式為:.gif.jpg.bmp的文件!";
          return false;
          }
          //-----------------------------
          //生成保存的文件路徑及文件名
          //-----------------------------
          strTemp = datTime.ToShortDateString().Replace( "-", "");
          FileName = datTime.ToLongTimeString().Replace(":","")+ Extension;
          FilePath = SavePath + strTemp + "/";
          Friendlib.Function.CreateDirectory(BasePath + FilePath);
          //保存大圖
          xImage=System.Drawing.Bitmap.FromStream(FileBox.PostedFile.InputStream);

          xImage.Save(BasePath+FilePath+FileName);
          xImage.Dispose();
          //-----------------------------
          //開始生成縮略圖
          //-----------------------------
          xBitmap=new Bitmap(BasePath+FilePath+FileName);//------------------

          PhotoHeight=xBitmap.Height;
          PhotoWidth=xBitmap.Width;
          // //判斷圖片的形狀
          if(Convert.ToDecimal(PhotoHeight)/Convert.ToDecimal(PhotoWidth)>Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))//高有余
          {
          PhotoHeight=Convert.ToInt16((Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))*Convert.ToDecimal(PhotoWidth));
          NewPhoto=new Rectangle(0,0,PhotoWidth,PhotoHeight);
          }
          else if(Convert.ToDecimal(PhotoHeight)/Convert.ToDecimal(PhotoWidth)<Convert.ToDecimal(ThumbnailHeight)/Convert.ToDecimal(ThumbnailWidth))
          {
          PhotoWidth=Convert.ToInt16((Convert.ToDecimal(ThumbnailWidth)/Convert.ToDecimal(ThumbnailHeight))*Convert.ToDecimal(PhotoHeight));
          NewPhoto=new
          Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width)-Convert.ToDecimal(PhotoWidth))/2),0,PhotoWidth,PhotoHeight);
          }
          else
          {
          NewPhoto= newRectangle(0,0,PhotoWidth,PhotoHeight); < BR> }
          System.Drawing.Image myBitmap;
          myBitmap=xBitmap.Clone(NewPhoto,System.Drawing.Imaging.PixelFormat.DontCare);
          System.Drawing.Image.GetThumbnailImageAbort myCallback = new
          System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
          System.Drawing.Image myThumbnail =
          myBitmap.GetThumbnailImage(ThumbnailWidth,ThumbnailHeight, myCallback,
          IntPtr.Zero); myThumbnail.Save(BasePath + FilePath +"s"+FileName,xObject);
          myThumbnail.Dispose(); myBitmap.Dispose(); xBitmap.Dispose(); return true; }
          else
          {
          blnResult=false; LastError="不能上傳大于2M及小于1.5K的圖片!";
          }
          }< BR> catch (Exception ex) {
          throw new System.Exception((ex.Message + ("\r\n" + ex.StackTrace)));
          }
          return blnResult;
          }
          private static bool ThumbnailCallback() { return false; }
          //http://www.aygfsteel.com/happyfish/archive/2005/05/11/4162.html 原文地址

          posted on 2008-04-23 19:10 cAng^Er 閱讀(490) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 根河市| 平昌县| 千阳县| 修水县| 法库县| 石景山区| 农安县| 隆昌县| 曲周县| 陆丰市| 土默特右旗| 余庆县| 龙川县| 南漳县| 平武县| 邛崃市| 新巴尔虎左旗| 康定县| 佛坪县| 惠水县| 台湾省| 霍林郭勒市| 邓州市| 东莞市| 铜山县| 南涧| 渝北区| 孙吴县| 阜平县| 东源县| 雷州市| 达尔| 宜春市| 白山市| 阳春市| 元氏县| 新昌县| 长垣县| 道孚县| 类乌齐县| 灯塔市|