2010-05-01 13 views
0

下面是一個上傳類的在C#腳本的一部分。我是一名PHP程序員,我從未與C#搞混,但我正在努力學習。這個上傳腳本不會處理除圖像以外的任何內容,我需要修改這個類來處理其他類型的媒體,或者一起重寫。如果我是正確的,我認識到,c#視頻相當於image.fromstream?或改變以下腳本的範圍,允許視頻太

using (Image image = Image.FromStream(file.InputStream)) 

基本上說,下面的範圍是圖片,只能使用一個圖像或對象被丟棄?也正被從文件流的圖片,我的理解是,像... PHP中的$ _FILES數組創建可變圖像?

我不知道,我現在不是真的在乎製作縮略圖,所以如果這個可以取出並仍然處理上傳,我完全很酷,我只是沒有得到任何運氣這個東西拿任何東西,但圖像,註釋掉類是整體的一部分,即使...

protected void Page_Load(object sender, EventArgs e) 
    { 
     string dir = Path.Combine(Request.PhysicalApplicationPath, "files"); 

     if (Request.Files.Count == 0) 
     { 
      // No files were posted 
      Response.StatusCode = 500; 
     } 
     else 
     { 
      try 
      { 
       // Only one file at a time is posted 
       HttpPostedFile file = Request.Files[0]; 

       // Size limit 100MB 
       if (file.ContentLength > 102400000) 
       { 
        // File too large 
        Response.StatusCode = 500; 
       } 
       else 
       { 
        string id = Request.QueryString["userId"]; 
        string[] folders = userDir(id); 

        foreach (string folder in folders) 
        { 
         dir = Path.Combine(dir, folder); 
         if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); 
        } 
        string path = Path.Combine(dir, String.Concat(Request.QueryString["batchId"], "_", file.FileName)); 
        file.SaveAs(path); 

        // Create thumbnail 
        int dot = path.LastIndexOf('.'); 
        string thumbpath = String.Concat(path.Substring(0, dot), "_thumb", path.Substring(dot)); 
        using (Image image = Image.FromStream(file.InputStream)) 
        { 
         // Find the ratio that will create maximum height or width of 100px. 
         double ratio = Math.Max(image.Width/100.0, image.Height/100.0); 

         using (Image thumb = new Bitmap(image, new Size((int)Math.Round(image.Width/ratio), (int)Math.Round(image.Height/ratio)))) 
         { 
          using (Graphics graphic = Graphics.FromImage(thumb)) 
          { 
           // Make sure thumbnail is not crappy 
           graphic.SmoothingMode = SmoothingMode.HighQuality; 
           graphic.InterpolationMode = InterpolationMode.High; 
           graphic.CompositingQuality = CompositingQuality.HighQuality; 

           // JPEG 
           ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[1]; 

           // 90% quality 
           EncoderParameters encode = new EncoderParameters(1); 
           encode.Param[0] = new EncoderParameter(Encoder.Quality, 90L); 

           // Resize 
           graphic.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height)); 

           // Save 
           thumb.Save(thumbpath, codec, encode); 
          } 
         } 
        } 

        // Success 
        Response.StatusCode = 200; 
       } 

      } 
      catch 
      { 
       // Something went wrong 
       Response.StatusCode = 500; 
      } 
     } 
    } 

回答

0

嗯,我不太清楚,如果你是想簡單地將文件保存到服務器或什麼。我不清楚目標。但是,如果您只是想將文件保存到服務器,看起來您已經使用「file.SaveAs(path);」完成了該操作。如果這是目標,那麼就把其他的東西和你的套裝撕掉。如果你需要對文件進行更細粒度的控制,你可以使用下面的代碼,根據需要使用不同的構造函數。

byte[] buffer; 
int bytesRead = 0; 
System.IO.FileStream fs = new System.IO.FileStream("myfile.whatever"); // not sure about your constructor, look this one up 
while((bytesRead = file.InputStream.Read(buffer, 0, 1024)) > 0) 
    fs.write(buffer, 0, bytesRead); 
fs.Close(); 

現在我沒有真正嘗試這個,但它應該給你一個很好的基礎,從中你可以開始。

+0

,如果你只是想將文件保存到服務器上,它似乎你已經與做「file.SaveAs(路徑);」。如果這是目標,那麼就把其他的東西和你的套裝撕掉。 這完全是我想太多,但它似乎並沒有接受任何不是一個圖像,所以我猜我的問題出在其它地方..謝謝 – Daniel 2010-05-01 17:30:11

0

基本上說, 以下的範圍的圖像,只能 要使用的圖像或對象被丟棄?

  1. 範圍在C#有點不同的含義。簡而言之,範圍指的是可以使用特定變量的代碼區域。

  2. 使用聲明儘快採取處置的對象的照顧,因爲它的執行完成。這意味着您在using塊中創建的Image對象將在該using塊的執行結束後立即從內存中分離出來。

  3. 如果您使用的是流從一個有效的圖像文件不是,該行代碼會拋出ArgumentException的。

如果要爲視頻文件創建縮略圖,可以使用IMediaDet接口。 Here是一篇介紹如何從視頻文件創建縮略圖的文章。