圖片上傳,並自動產生縮圖!

來源:互聯網
上載者:User
代碼如下:
  /// <summary>
  /// 上傳單個圖片。產生縮圖,jpg格式。
  /// 調用方法:protected System.Web.UI.HtmlControls.HtmlInputFile FilePath;
  /// 類名.uploadPhoto(FilePath.PostedFile.InputStream);  
  /// </summary>
  /// <param name="source"></param>
 
  public static string  uploadPhoto(System.IO.Stream source)
  {   
   string photoGuid=System.Guid.NewGuid().ToString();
   //string sourcePic=System.Web.HttpContext.Current.Server.MapPath("/Upload/Photo/" + photoGuid + ".jpg");
   string path =System.Web.HttpContext.Current.Server.MapPath("/");
   string sourcePic= path + System.Configuration.ConfigurationSettings.AppSettings["photoPath"] +   photoGuid + ".jpg";  
   string thumbPic=path +System.Configuration.ConfigurationSettings.AppSettings["thumbPath"] +  photoGuid + ".jpg";
//   int thumbWidth=120;
//   int thumbHeight=160;
   int thumbWidth=114;
   int thumbHeight=154;    //產生圖片id
   Image image = Image.FromStream(source);
   if(image.Height>600 || image.Width >600)
    Compress(image,sourcePic);
   else
   {
    FileStream srcfs = new FileStream(sourcePic,FileMode.Create);
    image.Save(srcfs, ImageFormat.Jpeg);
    srcfs.Close();
   }    //產生縮圖
   int tw = thumbWidth;
   int th = thumbHeight;
   if(image.Height / th > image.Width / tw)
   {
    tw = image.Width * th / image.Height;
   }
   else
   {
    th = image.Height * tw / image.Width;
   }
   Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth,thumbHeight,PixelFormat.Format32bppRgb).GetHbitmap());
   Graphics g = Graphics.FromImage(thumbImage);
   SolidBrush brush = new SolidBrush(Color.White);
   g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
   g.DrawImage(image, (thumbWidth - tw)/2, (thumbHeight -th)/2, tw, th);
   g.Dispose();
   FileStream thumbfs= new FileStream(thumbPic,FileMode.Create);
   thumbImage.Save(thumbfs,ImageFormat.Jpeg);
   
   thumbfs.Close();
   image.Dispose();   
   return(photoGuid);
  }
  public static void Compress(Image sourceImage, string filename)
  {
   int targetWidth = 600;
   int targetHeight = 600;    if(sourceImage.Height <= 600 && sourceImage.Width <= 600)
   {
    targetHeight = sourceImage.Height;
    targetWidth = sourceImage.Width;
   }
   else if(sourceImage.Height > sourceImage.Width)
   {
    targetWidth = sourceImage.Width * 600 / sourceImage.Height;
   }
   else
   {
    targetHeight = sourceImage.Height * 600 / sourceImage.Width;
   }    // 縮放圖片
   Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());
   Graphics g = Graphics.FromImage(targetImage);
   g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);
   g.Dispose();
   // 設定輸出格式
   EncoderParameters encParams = new EncoderParameters(1);
   encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
   ImageCodecInfo codeInfo = null;
   ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();
   foreach(ImageCodecInfo info in codeInfos)
   {
    if(info.MimeType.Equals("image/jpeg"))
    {
     codeInfo = info;
     break;
    }
   }
   if(codeInfo != null)
   {
    targetImage.Save(filename, codeInfo, encParams);
   }
   targetImage.Dispose();
   
  }

Web.Config中的設定如下:
 <appSettings>
  <add key="photoPath" value="\upload\photo\"/>
 <add key="thumbPath" value="\upload\thumb\"/>
 <add key="otherFilePath" value="\upload\other\"/> </appSettings>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.