using System;
          using System.Web;
          using System.Web.UI;
          using System.IO;
          using System.Drawing;
          using System.Drawing.Imaging;
          using System.Drawing.Drawing2D;
          using System.Text.RegularExpressions;

          namespace BaseProject.Classes.Util
          {
              public class UpLoad
              {
                  /// <summary>
                  /// 指定路徑 圖片大小
                  /// </summary>
                  /// <param name="dir"></param>
                  /// <param name="width"></param>
                  /// <param name="height"></param>
                  /// <returns></returns>
                  public string GetSqlFile(string dir, int width, int height)
                  {
                      string sqlfile = "";
                      string path = HttpContext.Current.Request.PhysicalApplicationPath.ToString() + "upload/";
                      path += dir;
                      if (Directory.Exists(path))
                      {

                      }
                      else
                      {
                          Directory.CreateDirectory(path);
                      }
                      if (Directory.Exists(path + "/small"))
                      {
                      }
                      else
                      {
                          Directory.CreateDirectory(path + "/small");
                      }
                      HttpFileCollection files = HttpContext.Current.Request.Files;
                      if (files[0].FileName.ToString().Length > 0)
                      {
                        
                          string filename = files[0].FileName.ToString();
                          string datestr = new StringOperate().ConvertChineseCharacterToSpelling(Regex.Replace(filename, "[^\\u4e00-\\u9fa5]", ""))+DateTime.Now.ToString("yyyyMMddHmmfff") ;
                          string ext = filename.Substring(filename.LastIndexOf(".")).ToLower();
                          if (ext != ".bmp" && ext != ".jpg" && ext != ".gif" && ext != ".jpeg")
                          {
                              HttpContext.Current.Response.Write("<script>alert('上傳的文件不是.gif,jpg,jpeg,bmp格式')</script>");
                              return "";
                          }
                          files[0].SaveAs(path + "/" + datestr + ext);
                          #region 生成小圖
                          string originalFilename = path + "/" + datestr + ext;
                        
                          //縮小的倍數(shù)
                          int iScale = 1;
                          //從文件取得圖片對(duì)象
                          Image image = null;
                          try
                          {
                              image = Image.FromFile(originalFilename);
                          }
                          catch
                          {
                              //
                              try
                              {
                                  File.Delete(originalFilename);
                                  image.Dispose();

                              }
                              catch
                              {
                              }
                              HttpContext.Current.Response.Write("<script>alert('上傳的文件不是.gif,jpg,jpeg,bmp圖片的標(biāo)準(zhǔn)格式格式')</script>");
                              return "";

                          }
                          int hi = 0;
                          int wi = 0;
                          wi = width;
                          hi = height;
                          Size size = new Size(wi, hi);
                          //新建一個(gè)bmp圖片
                          Image bitmap = new Bitmap(size.Width, size.Height);
                          //新建一個(gè)畫(huà)板
                          Graphics g = Graphics.FromImage(bitmap);
                          //設(shè)置高質(zhì)量插值法
                          g.InterpolationMode = InterpolationMode.High;
                          //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
                          g.SmoothingMode = SmoothingMode.HighQuality;
                          //清空一下畫(huà)布
                          g.Clear(Color.Blue);
                          //在指定位置畫(huà)圖
                          g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                          if (ext == ".jpg" || ext == ".jpeg")
                              bitmap.Save(path + "/small/" + datestr + ext, ImageFormat.Jpeg);
                          if (ext == ".gif")
                              bitmap.Save(path + "/small/" + datestr + ext, ImageFormat.Gif);
                          if (ext == ".bmp")
                              bitmap.Save(path + "/small/" + datestr + ext, ImageFormat.Bmp);
                          image.Dispose();
                          bitmap.Dispose();
                          g.Dispose();
                          #endregion
                          sqlfile = datestr + ext;

                          try
                          {
                              image.Dispose();
                              bitmap.Dispose();
                              g.Dispose();
                          }
                          catch (Exception ex)
                          {
                              string exc = ex.Message.ToString();
                              HttpContext.Current.Response.Write("<script>alert('" + exc + "');</script>");
                          }
                      }
                      else
                      {
                          sqlfile = "";
                      }
                      return sqlfile;
                  }
                  /// <summary>
                  /// 指定路徑 圖片大小
                  /// </summary>
                  /// <param name="dir"></param>
                  /// <param name="width"></param>
                  /// <param name="height"></param>
                  /// <returns></returns>
                  public string GetSqlFileSmallImage(string dir, int width, int height)
                  {
                      string sqlfile = "";
                      string path = HttpContext.Current.Request.PhysicalApplicationPath.ToString() + "upload/";
                      path += dir;
                      if (Directory.Exists(path))
                      {

                      }
                      else
                      {
                          Directory.CreateDirectory(path);
                      }
                      if (Directory.Exists(path + "/small"))
                      {
                      }
                      else
                      {
                          Directory.CreateDirectory(path + "/small");
                      }
                      HttpFileCollection files = HttpContext.Current.Request.Files;
                      if (files[0].FileName.ToString().Length > 0)
                      {
                          string filename = files[0].FileName.ToString();
                          string tempFileName = filename.Substring(filename.LastIndexOf('\\') + 1, filename.LastIndexOf('.') - (filename.LastIndexOf('\\') + 1));
                          string datestr = new StringOperate().ConvertChineseCharacterToSpelling(Regex.Replace(filename, "[^\\u4e00-\\u9fa5]", "")) + DateTime.Now.ToString("yyyyMMddHmmfff");
                          string ext = filename.Substring(filename.LastIndexOf(".")).ToLower();
                          if (ext != ".bmp" && ext != ".jpg" && ext != ".gif" && ext != ".jpeg")
                          {
                              HttpContext.Current.Response.Write("<script>alert('上傳的文件不是.gif,jpg,jpeg,bmp格式')</script>");
                              return "";
                          }
                          files[0].SaveAs(path + "/" + datestr + ext);
                          #region 生成小圖
                          string originalFilename = path + "/" + datestr + ext;
                        
                          //縮小的倍數(shù)
                          //int iScale = 1;
                          //從文件取得圖片對(duì)象
                          Image image = null;
                          try
                          {
                              image = Image.FromFile(originalFilename);
                          }
                          catch
                          {
                              //
                              try
                              {
                                  File.Delete(originalFilename);
                                  image.Dispose();

                              }
                              catch
                              {
                              }
                              HttpContext.Current.Response.Write("<script>alert('上傳的文件不是.gif,jpg,jpeg,bmp圖片的標(biāo)準(zhǔn)格式格式')</script>");
                              return "";

                          }
                          int hi = 0;
                          int wi = 0;
                          wi = width;
                          hi = height;
                          Size size = new Size(wi, hi);
                          //新建一個(gè)bmp圖片
                          Image bitmap = new Bitmap(size.Width, size.Height);
                          //新建一個(gè)畫(huà)板
                          Graphics g = Graphics.FromImage(bitmap);
                          //設(shè)置高質(zhì)量插值法
                          g.InterpolationMode = InterpolationMode.High;
                          //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
                          g.SmoothingMode = SmoothingMode.HighQuality;
                          //清空一下畫(huà)布
                          g.Clear(Color.Blue);
                          //在指定位置畫(huà)圖
                          g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                          if (ext == ".jpg" || ext == ".jpeg")
                              bitmap.Save(path + "/small/" + datestr + ext, ImageFormat.Jpeg);
                          if (ext == ".gif")
                              bitmap.Save(path + "/small/" + datestr + ext, ImageFormat.Gif);
                          if (ext == ".bmp")
                              bitmap.Save(path + "/small/" + datestr + ext, ImageFormat.Bmp);
                          image.Dispose();
                          bitmap.Dispose();
                          g.Dispose();
                          #endregion
                          sqlfile = datestr + ext;

                          try
                          {
                              image.Dispose();
                              bitmap.Dispose();
                              g.Dispose();
                              System.IO.File.Delete(path + "/" + datestr + ext);
                          }
                          catch (Exception ex)
                          {
                              string exc = ex.Message.ToString();
                              HttpContext.Current.Response.Write("<script>alert('" + exc + "');</script>");
                          }
                      }
                      else
                      {
                          sqlfile = "";
                      }
                      return sqlfile;
                  }

              }
          }

          posted on 2009-04-02 18:36 sanmao 閱讀(748) 評(píng)論(1)  編輯  收藏

          FeedBack:
          # re: 上傳圖片類(lèi)代碼
          2011-03-10 17:33 | 君不悔
          這是java代碼?  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          常用鏈接

          留言簿(5)

          隨筆分類(lèi)

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 内江市| 霍州市| 红桥区| 商水县| 洮南市| 鄯善县| 尼勒克县| 崇明县| 靖边县| 古田县| 泸州市| 武强县| 赤壁市| 安乡县| 上栗县| 香港 | 延川县| 曲周县| 利津县| 嘉峪关市| 镇沅| 神池县| 灵寿县| 庄河市| 衡水市| 华宁县| 岑溪市| 双柏县| 平果县| 平泉县| 神木县| 舒城县| 香河县| 竹山县| 白河县| 榆社县| 丹江口市| 新巴尔虎右旗| 正宁县| 余干县| 马龙县|