<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    happyfish

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      38 Posts :: 1 Stories :: 62 Comments :: 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; }

    posted on 2005-05-11 10:34 小魚兒 閱讀(4139) 評論(8)  編輯  收藏 所屬分類: encoding

    Feedback

    # re: 上傳圖片并生成略縮圖 2006-02-22 14:48 dddffgg
    gfhgh  回復  更多評論
      

    # http://comic.qq.com/z/wenchuan/images/poster/feel001.jpg 2006-03-16 11:51 k
    hjgkjhg  回復  更多評論
      

    # re: 上傳圖片并生成略縮圖 2006-03-16 11:51 k
    tg  回復  更多評論
      

    # re: 上傳圖片并生成略縮圖 2006-06-16 13:18 sa
    您要上傳的圖片小于5K,請上傳足夠尺寸和清晰度的掃描件  回復  更多評論
      

    # re: 上傳圖片并生成略縮圖 2006-10-29 10:18 WFWEWE
    @sa
    EWFEWFEWF  回復  更多評論
      

    # re: 上傳圖片并生成略縮圖 2006-11-06 03:10 愛ぁ隨★云
    didfoh
      回復  更多評論
      

    # re: 上傳圖片并生成略縮圖 2006-11-06 03:17 愛ぁ隨★云
    454  回復  更多評論
      

    # re: 上傳圖片并生成略縮圖 2007-03-14 21:00 劉套
    好好的  回復  更多評論
      

    主站蜘蛛池模板: 国产精品亚洲精品日韩已方| 国产黄在线播放免费观看| 丁香花在线视频观看免费| 免费又黄又硬又爽大片| 亚洲日韩一区二区一无码| 噼里啪啦免费观看高清动漫4| 亚洲人成在线观看| 午夜视频免费在线观看| 亚洲黄色片在线观看| 18pao国产成视频永久免费| 亚洲欧洲精品一区二区三区| 久久国产色AV免费看| 久久亚洲AV成人无码| 成人免费午夜在线观看| 亚洲高清一区二区三区电影| 亚洲第一网站男人都懂| 中文字幕a∨在线乱码免费看| 国产成A人亚洲精V品无码性色| 久久久久免费看黄a级试看| 亚洲尹人九九大色香蕉网站| 亚洲精品免费在线视频| 亚洲一卡2卡3卡4卡5卡6卡 | 亚洲国产另类久久久精品小说| japanese色国产在线看免费| 久久精品国产亚洲| 国产一卡2卡3卡4卡2021免费观看| 亚洲色大18成人网站WWW在线播放| 国产在线观看免费完整版中文版 | 香蕉视频在线观看免费| 国产成人麻豆亚洲综合无码精品| 久久午夜夜伦鲁鲁片免费无码| 久久亚洲精品无码VA大香大香| 狼友av永久网站免费观看| h视频免费高清在线观看| 亚洲Av永久无码精品三区在线| 国产成在线观看免费视频| 国产精品亚洲天堂| 久久狠狠高潮亚洲精品| 日本高清免费aaaaa大片视频| 你好老叔电影观看免费| 涩涩色中文综合亚洲|