从byte []创建图像

时间:2017-02-17 09:43:13

标签: c# arrays image-processing asp.net-core-mvc

我是一名.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活动

这是来自命名空间System.Drawing的Image类(不是我自己的类):

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会变成红色的波浪形。

告诉我,如果您希望从我的代码中看到更多内容,我会带来它。

提前致谢所有帮助

1 个答案:

答案 0 :(得分: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})" />