2014-11-21 41 views
1

我試圖將現有的WinForms應用程序轉換爲MVC Web應用程序。在瀏覽器中使用的控制器返回圖像

我正在嘗試顯示控制器的圖像。

此刻,我從一個函數中獲得了一個System.Drawing.Image,我正在試圖解決如何將這個提供給View。

這是我的控制器:

public System.Drawing.Image getStudentImage(string PersonID) 
{ 
     System.Drawing.Image studentImage = ClientFunctions.GetStudentImage(GlobalVariables.networkstuff, GlobalVariables.testAuth, PersonID); 
     return studentImage; 
} 

然後,在我看來,我想顯示此圖像...

希望是這樣的:

<img id="@pupil.PupilInfo.PersonID" src="~/TakeRegister/getStudentImage/@pupil.PupilInfo.PersonID" alt="" /> 

當我運行代碼我在瀏覽器中看到了破碎的圖像,當我查看圖像的「src」時,它看起來像這樣:

http://localhost:19340/TakeRegister/getStudentImage/3058 

我得到我這樣做超過1個東西錯在這裏的感覺......(?)

誰能給我任何指針嗎?

+0

http://bit.ly/1vvAdl7的[渲染爲System.Drawing.Image在ASP.NET MVC 3查看最佳方式( – 2014-11-21 12:25:43

+1

可能重複http://stackoverflow.com/questions/11546865/最好的方式來渲染系統繪圖圖像在ASP網絡mvc 3視圖) – 2014-11-21 12:26:20

+0

@AntP謝謝! - 完美的作品...我很明顯在谷歌措辭不正確的我的問題,因爲我沒有找到:( – 2014-11-21 12:40:11

回答

5

您可以在您的操作中使用​​。

public ActionResult Image(int PersonID) 
     { 
      System.Drawing.Image image = ClientFunctions.GetStudentImage(GlobalVariables.networkstuff, GlobalVariables.testAuth, PersonID); 

      using (var ms = new MemoryStream()) 
      { 
       image.Save(ms, ImageFormat.Jpeg); 

       return File(ms.ToArray(), "image/jpeg"); 
      } 
     }