2012-09-25 52 views
0

我希望有人幫助我在這個問題..異常服務器上讀取文件的圖像時 - ASP.NET

我有簡單的ASPX代碼從目錄中的圖像文件,並在它的一些proccesses因爲版上繪製的財產以後之後,我將它保存在同一個目錄中。

真的代碼在本地機器上運行良好,但在服務器端代碼失敗並出現異常。 (ArgumentException的:參數是無效的)

請看代碼:

DirectoryInfo[] dir = new DirectoryInfo[2]; 
dir[0] = new DirectoryInfo(Server.MapPath("Image/DB/Large/")); 
dir[1] = new DirectoryInfo(Server.MapPath("Image/DB/Thumb/")); 

System.Drawing.Image signature = System.Drawing.Image.FromFile(Server.MapPath("Image/Design/signature.png")); 
for (int i = 0; i < dir.Length; i++) 
{ 
    FileInfo[] fs = dir[i].GetFiles("*.jpg"); 
    foreach (FileInfo s in fs) 
    { 
     FileStream strm = s.OpenRead(); 
     String name = s.Name; 
     System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 
     Graphics g = Graphics.FromImage(img); 
     g.SmoothingMode = SmoothingMode.HighQuality; 
     g.DrawImage(signature, new Point(0, 0));  
     strm.Close(); 
     if (i == 0) 
     { 
      String v = Server.MapPath("Image/DB/Large/" + name); 
      img.Save(v); 
     } 
     else if (i == 1) 
     { 
      String v = Server.MapPath("Image/DB/Slide/" + name); 
      img.Save(v); 
     } 
     g.Dispose();   
    } 
} 

異常詳細信息:

參數無效。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.ArgumentException:參數無效。

Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

    Stack Trace: 


[ArgumentException: Parameter is not valid.] 
    System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +1062843 
    System.Drawing.Image.FromStream(Stream stream) +8 
    Developer.Button1_Click(Object sender, EventArgs e) +279 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 

回答

3

必須轉換的FileStream到流

System.Drawing.Image.FromStream(..);// This method take Stream as argument and nor FileStream 

您可以嘗試CopyTo從,爲了轉換。

鏈接:http://msdn.microsoft.com/en-us/library/dd782932.aspx

FileStream strm = s.OpenRead(); 
Stream stream = new Stream(); 
strm.CopyTo(stream); 
System.Drawing.Image.FromStream(stream); 

.NET 4中之前,您可以使用此擴展方法

公共靜態類分機 {

public static void CopyTo(this FileStream in, Stream out) 
{ 
    byte[] temp= new byte[16*1024]; //You can adjust this value 
    int bytesRead; 

    while ((bytesRead = in.Read(temp,0, temp.Length)) > 0) 
    { 
     out.Write(temp, 0, bytesRead); 
    } 
} 

}

+0

感謝Aghilas Yakoub。 。但我使用.net 3.5 ..有沒有其他的方式來做到這一點。 –

+0

穆罕默德我高興地幫助你,我更新你的代碼 –

+0

我做下面的代碼如下,但仍然是例外..我不知道爲什麼! ? 'FileStream strm = s.OpenRead(); String name = s.Name; MemoryStream stream = new MemoryStream(); CopyTo(strm,stream); System.Drawing.Image img = System.Drawing.Image.FromStream(stream);' –

相關問題