以字节形式存储在数据库中的图像在表中不显示为图像,而是显示为字节

本文关键字:显示 图像 字节 存储 数据库 | 更新日期: 2023-09-27 18:28:38

我正试图以字节数组格式将图像保存到数据库中,但当我查看数据库时,我只看到字节,而没有看到图像!我如何才能做到不看字节码,只看图像?我必须在客户端反向处理吗?请帮忙!非常感谢。

字节转换功能:

private byte[] String_To_Bytes2(string strInput)
        {
            int numBytes = (strInput.Length) / 2;
            byte[] bytes = new byte[numBytes];
            for (int x = 0; x < numBytes; ++x)
            {
                bytes[x] = Convert.ToByte(strInput.Substring(x * 2, 2), 16);
            }
            return bytes;
        }

以字节形式存储在数据库中的图像在表中不显示为图像,而是显示为字节

我使用以下帖子中的第一个解决方案解决了这个问题:在Razor/MVC3中显示数据库图像(字节[])?

模型。内容是一个字节[]图像。

@{
    string imageBase64 = Convert.ToBase64String(Model.Content);
    string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);}  <img src="@imageSrc" alt="@Model.Name" width="100" height="100" />