我有这个模型,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/