c# - ASP.NET - Razor 从模型中获取图像

标签 c# html asp.net razor

我有这个模型,imageFirst 和 imageLast 将是来自 Properties.Resources 的图像

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

namespace Clinica_Imagine_Web.Models.ClinicCase {

    public class ClinicCase {

        public Bitmap imageFirst { get; set; }
        public Bitmap imageLast { get; set; }
        public string description { get; set; }
    }
}

在 htmlview 中我有这个例子

@using Clinica_Imagine_Web.Models.ClinicCase
@model ClinicCase

<h2>ClinicCase</h2>

<p>@Model.description</p>
<img src="@Model.imageFirst" />
<img src="@Model.imageLast" />

如何在 htmlview 中显示模型的图像?

最佳答案

在模型中使用 byte[] 数组代替位图。比你将能够使用这个:

<img src="@String.Format("data:image/png;base64,{0}", Convert.ToBase64String(Model.ImageBytes))" />

这就是将位图转换为数组的方式:

private static byte[] BitmapToBytes(Bitmap bitmap)
{
    using (var stream = new MemoryStream())
    {
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        return stream.ToArray();
    }
}

关于c# - ASP.NET - Razor 从模型中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323248/

相关文章:

c# - Lucene.Net(3.0.3 或 4.8.0)QueryParser 可以搜索数字吗?

c# - 计划的 WebJob 未按计划调用函数

c# - Stored proc输出参数+Select不传回输出参数

html - HTML5如何支持RDFa?

javascript - jQuery 背景图片更改,需要帮助

javascript - 之后的代码不能完全使用 jquery、javascript。我很困惑:(

c# - 根据参数类型调用方法

.net - 使用前端集合时为"The data source does not support server-side data paging"

c# - Task.Run 在 ASP .NET MVC Web 应用程序中被认为是不好的做法吗?

c# - System.Web.Helpers HashPassword 盐和迭代?