2017-02-17 165 views
0

我是一名.NET學生,試圖學習如何從數據庫中獲取圖像(varbinary(MAX))並將其轉換爲圖像文件。我已成功設法將圖像轉換爲byte []並將其保存到數據庫。這是到目前爲止我的代碼試圖讓圖像回:從字節創建圖像[]

public IActionResult Index() 
    { 

     var kund = DbContext.Kunder.SingleOrDefault(p => p.KundId == 2); 
     byte[] byteImage = kund.Bild; 

     Image image = byteArrayToImage(byteImage); 

     return View(); 
    } 

    public Image byteArrayToImage(byte[] byteArrayIn) 
    { 
     MemoryStream ms = new MemoryStream(byteArrayIn); 
     Image returnImage = Image.FromStream(ms); 
     return returnImage; 
    } 

我有我所知道的問題之一是,Visul Studio正在講述這件事:

嚴重性代碼說明項目文件的線路抑制狀態 錯誤CS0117'圖像'不包含 的定義'FromStream'ImageToDB..NETCoreApp,Version = v1.0 D:\ Kay \ ASP.NET MVC \ ImageToDatabase \ ImageToDB \ src \ ImageToDB \ Controllers \ HomeController.cs 40 Active

這是從命名空間System.Drawing中的圖像類(不是我自己的類):

namespace System.Drawing 
{ 
    public abstract class Image 
    { 
     protected Image(); 
solution 
     public delegate bool GetThumbnailImageAbort(); 
    } 
} 

我的依賴關係:

{ 
    "dependencies": { 
    "Microsoft.NETCore.App": { 
     "version": "1.0.1", 
     "type": "platform" 
    }, 
    "Microsoft.AspNetCore.Diagnostics": "1.0.0", 
    "Microsoft.AspNetCore.Mvc": "1.0.1", 
    "Microsoft.AspNetCore.Razor.Tools": { 
     "version": "1.0.0-preview2-final", 
     "type": "build" 
    }, 
    "Microsoft.AspNetCore.Routing": "1.0.1", 
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", 
    "Microsoft.AspNetCore.StaticFiles": "1.0.0", 
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0", 
    "Microsoft.Extensions.Logging": "1.1.0", 
    "Microsoft.Extensions.Logging.Console": "1.0.0", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0", 
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", 
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0", 
    "Microsoft.EntityFrameworkCore": "1.1.0", 
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0", 
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final", 

    }, 

    "tools": { 
    "BundlerMinifier.Core": "2.0.238", 
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final", 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" 
    }, 

    "frameworks": { 
    "netcoreapp1.0": { 
     "imports": [ 
     "dotnet5.6", 
     "portable-net45+win8" 
     ] 
    } 
    }, 

當我看到不同勢的例子在網絡上我經常看到這解決方案,但是當我嘗試使用它時,FromStream會呈現紅色的波浪狀。

告訴我,如果還有什麼更多的,你想從我的代碼中看到,我會帶上它。

在此先感謝所有的幫助

+0

當你說「這是Image類」 - 你的意思是你已經創建了自己的圖像類?如果是這樣,不要這樣做......如果沒有,請澄清。 (另外,請說明你的.NET版本是什麼,看起來像是.NET Core,但是哪個版本?) –

+0

對不起。我沒有創建這個類。將澄清 – AllramEst

+3

可能重複的[轉換字符圖像在C銳淨](http://stackoverflow.com/questions/2623198/convert-bytes-to-image-in-c-sharp-net) –

回答

0

我破解案件。我的解決方案

public IActionResult Index() 
    { 

     var kund = DbContext.Kunder.SingleOrDefault(p => p.KundId == 1); 
     //byte[] byteArray = DbContext.Kunder.Find(kund).Bild; 
     byte[] byteImage = kund.Bild; 


     return File(byteImage, "image/jpg"); 
    } 

查看

 @model ImageToDB.Models.Kund 

<img src="@Url.Action("RenderImage", new { id = Model.Bild})" />