使用 ASP.Net MVC3 显示包含在 byte[] 中的图像

发布于 2024-11-08 07:21:35 字数 114 浏览 0 评论 0原文

我的观点很强烈。这个强类型有一个由byte[]组成的字段,这个数组包含一张图片。

是否可以使用 @Html.Image(Model.myImage) 之类的内容显示此图像?

非常感谢

I've a view with a strong type. This strong type has a field consisting of a byte[], this array contains a picture.

Is it possible to display this image with something like @Html.Image(Model.myImage) ?

Thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

揪着可爱 2024-11-15 07:21:35

您可以创建一个控制器操作方法,该方法将图像作为 FileContentResult 返回:

public FileContentResult Display(string id) {
   byte[] byteArray = GetImageFromDB(id);
   return new FileContentResult(byteArray, "image/jpeg");
}

然后您可以使用模型中的图像 ID 在视图中创建指向操作方法的 ActionLink。

You can create a controller action method that returns an image as a FileContentResult:

public FileContentResult Display(string id) {
   byte[] byteArray = GetImageFromDB(id);
   return new FileContentResult(byteArray, "image/jpeg");
}

Then you can create an ActionLink to the action method in the view using the image id from the model.

蹲在坟头点根烟 2024-11-15 07:21:35

这取决于图像有多大。如果它很小,您可以编写一些内容对其进行 base-64 编码并将其嵌入 html 中,喜欢其中任何一个

对于具体示例< /a>:

<img src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAAAAABQAA8AAAPbWLrc/jDKSVe4OOvNu/9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVrApTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KTkpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxMAF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=" alt="British Blog Directory" width="80" height="15">

如果图像具有任何明显的大小,您可能需要编写一条允许通过图像的某个键进行查找的路由,即类似 /images/{id} 的路由 - 在您获取图像二进制文件并使用 return File(bytes, contentType) 的路线,另外设置缓存标头(并记住重新检查任何必要的安全性)。在你的 html 中你只会有一个

<img src="/images/@imageId" ... />

(使用 razor 语法,但与 aspx 类似)。

单独的路由方法需要到服务器的额外跃点,但允许在客户端进行缓存(内联 base-64 方法将数据放在每个请求上)。

It depends on how big the image is. If it is small, you could write something to base-64 encode it and embed it in the html, like any of these.

For a concrete example from here:

<img src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAAAAABQAA8AAAPbWLrc/jDKSVe4OOvNu/9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVrApTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KTkpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxMAF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=" alt="British Blog Directory" width="80" height="15">

If the image is of any appreciable size, you may want instead to write a route that allows lookup via some key to the image, i.e. a route like /images/{id} - in that route you fetch the image binary and use return File(bytes, contentType), additionally setting caching headers (and remember to re-check any necessary security). In your html you would just have an

<img src="/images/@imageId" ... />

(using razor syntax, but similar for aspx).

The separate route approach requires an additional hop to the server, but allows caching at the client (the inline base-64 approach puts the data on every request).

好菇凉咱不稀罕他 2024-11-15 07:21:35

如果您已经将图像作为 byte[] 数组加载到模型中,您可以按照 @Marc Gravell 在 他的回答:

<img src="data:image;base64,@System.Convert.ToBase64String(Model.Photo)" />

这极大地简化了整个过程,您不需要有特定的 FileContentResult 操作方法并再次访问数据库(请参阅@Dmitry S 的answer) 只是为了获取带有图像/照片的 byte[] 数组,因为您已经有了它加载到您的模型中。

If you already happen to have the image loaded in your model as a byte[] array you can do this as @Marc Gravell mentions in his answer:

<img src="data:image;base64,@System.Convert.ToBase64String(Model.Photo)" />

This greatly simplifies the whole process and you won't need to have a specific FileContentResult action method and hit the database again (see @Dmitry S's answer) just to fetch that byte[] array with your image/photo since you already have it loaded in your model.

手长情犹 2024-11-15 07:21:35

听起来您需要一个新操作来获取字节数组(从数据库?)并通过 File 方法....

然后生成一个指向该action的锚点,这样可以在页面加载的同时加载图片,加快显示速度。

Sounds like you'd need a new action that gets the byte array (from a database?) and returns the image via the File method....

Then generate an anchor that points to that action, that way the image can be loaded while the page is loading, speeding up the display.

梅倚清风 2024-11-15 07:21:35

我会设计一个简单的通用处理程序来为您的图像提供服务。在给定一些参数的情况下,该处理程序可以从数据库加载图像,并将它们写入 http 输出流。

public class UserImage : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpeg";

        // Get the stream from the database
        var image = System.Drawing.Image.FromStream(stream);

        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }
}

I would design a simple generic handler for serving your images. This handler could, given some parameter, load images from the database, and write them to the http output stream.

public class UserImage : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpeg";

        // Get the stream from the database
        var image = System.Drawing.Image.FromStream(stream);

        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文