代码之家  ›  专栏  ›  技术社区  ›  xDevil

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

  •  0
  • xDevil  · 技术社区  · 10 年前

    我试图以字节数组格式将图像保存到数据库中,但当我查看数据库时,我只看到字节,而看不到图像!我怎样才能做到我看不到字节码而是看到图像?我是否必须在客户端反向执行此过程? 请帮忙!非常感谢。

    字节转换功能:

    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;
            }
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Community Mohan Dere    8 年前

    我通过使用以下帖子中的第一个解决方案成功解决了这个问题: Displaying database image (bytes[]) in 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" />