asp.net显示来自字节数组的图像 [英] asp.net display image from byte array

查看:465
本文介绍了asp.net显示来自字节数组的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组,并试图从中显示图像。

I have a byte array and trying to display image from that.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

namespace RealPortableTerminal
{
public partial class resim : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PortableTerminalDbEntities entity = new PortableTerminalDbEntities();
        byte[] arr = (from b in entity.Sicil where b.Id == 31 select b.Fotograf).First();

        Image rImage = null;
        using (MemoryStream ms = new MemoryStream(arr))
        {
            rImage = Image.FromStream(ms);
        }
    }
}
}

它强调FromStream并说'System.Web.UI.WebControls.Image'不包含'FromStream'的定义。似乎添加System.Data.Linq引用没有改变任何东西。我错过了什么吗?顺便说一句我很确定我正确地从数据库中获取字节数组。

It underlines FromStream and says 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream'. It seems adding System.Data.Linq reference did not changed anything. Am I missing something ? Btw I am pretty sure I take byte array from database correctly.

推荐答案

另一种方法是将字节数组转换为一个base 64字符串,并将其分配给 rImage ImageUrl 属性,如下所示:

Another way to do it is to convert your byte array into a base 64 string and assign that to the ImageUrl property of rImage, like so:

rImage.ImageUrl = "data:image;base64," + Convert.ToBase64String(arr);

您不需要中间 MemoryStream 或单独的页面...如果字节数组是浏览器支持的图像格式,它将显示。祝你好运。

You don't need the intermediate MemoryStream or a separate page...if the byte array is in an image format the browser supports, it will display. Good luck.

这篇关于asp.net显示来自字节数组的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆