.net 上傳圖片封裝類

來源:互聯網
上載者:User
 

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

namespace webdb.classes
{
 /// <summary>
 /// UploadFile 的摘要說明。
 /// </summary>
 public class UploadFile
 {
  public UploadFile()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }
  /// <summary>
  /// 無參數的上傳檔案
  /// </summary>
  /// <returns></returns>
  public string GetSqlFile()
  {
   
   string sqlfile="";
   string path = HttpContext.Current.Request.PhysicalApplicationPath.ToString()+"/upload";
   if(Directory.Exists(path))
   {

   }
   else
   {
    Directory.CreateDirectory(path);
   }
   HttpFileCollection files = HttpContext.Current.Request.Files;
   if(files[0].FileName.ToString().Length>0)
   {
    string filename = files[0].FileName.ToString();
    string datestr  = DateTime.Now.ToString("yyyyMMddHmmss")+DateTime.Now.Millisecond;
    string ext     = filename.Substring(filename.LastIndexOf("."));
    files[0].SaveAs(path+"file://"+datestr+ext/);
    sqlfile=datestr+ext;
   }
   else
   {

   }
   return sqlfile;
  }
  public string GetSqlFile(string dir)
  {
   string sqlfile="";
   string path = HttpContext.Current.Request.PhysicalApplicationPath.ToString()+"/upload/";
   path +="file://"+dir/;
   if(Directory.Exists(path))
   {

   }
   else
   {
    Directory.CreateDirectory(path);
   }
   HttpFileCollection files = HttpContext.Current.Request.Files;
   
  
   if(files[0].FileName.ToString().Length>0)
   {
    string filename = files[0].FileName.ToString();
    string datestr  = DateTime.Now.ToString("yyyyMMddHmmss")+DateTime.Now.Millisecond;
    string ext     = filename.Substring(filename.LastIndexOf("."));
    files[0].SaveAs(path+"/"+datestr+ext);
    sqlfile=datestr+ext;
   }
   else
   {

   }
   return sqlfile;
  }
  /// <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 +="file://"+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  = DateTime.Now.ToString("yyyyMMddHmmss")+DateTime.Now.Millisecond;
    string ext     = filename.Substring(filename.LastIndexOf("."));
    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+"file://"+datestr+ext/;
    //產生的高品質圖片名稱
    string strGoodFile = path+"file://"+datestr+ext/;
    //產生的低品質圖片名稱
    //string strBadFile = mPath + "\\" + newname;
    //縮小的倍數
    int iScale = 1;
    //從檔案取得圖片對象
    Image image = Image.FromFile(originalFilename);
    //取得圖片大小
    //Size size = new Size(image.Width/iScale, image.Height/iScale);
    int hi =0;
    int wi =0;
    //if(image.Width>100)
    //{
    wi=width;
    //}
    //else
    //{

    //}
    //if(image.Height>55)
    //{
    hi=height;
    //}
    //else
    //{

    //}
    Size size = new Size(wi,hi);
    //建立一個bmp圖片
    Image bitmap = new Bitmap(size.Width, size.Height);
    //建立一個畫板
    Graphics g = Graphics.FromImage(bitmap);
    //設定高品質插值法
    g.InterpolationMode = InterpolationMode.High;
    //設定高品質,低速度呈現平滑程度
    g.SmoothingMode = SmoothingMode.HighQuality;
    //清空一下畫布
    g.Clear(Color.Blue);
    //在指定位置畫圖
    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
   {

   }
   return sqlfile;
  }
 }
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.