【问题标题】:How to display mysql blob image in asp.net image control?如何在asp.net图像控件中显示mysql blob图像?
【发布时间】:2012-09-25 07:47:15
【问题描述】:

我知道在 Windows 窗体中显示 mysql blob 图像的方法。

try
            {
                MySqlConnection connection = new MySqlConnection(hp.myConnStr);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = "select logo from mcs_institude where id = 1";
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    pictureBox1.Image = new Bitmap(new MemoryStream((byte[])Reader.GetValue(0)));
                }
                connection.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error in Get_ImageFormDB"+ ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

但现在我在做一个 asp.net 项目。在这个图像中没有图像属性。

command = connection.CreateCommand();
            command.CommandText = "Select FO_Roomdet_Image from fo_roomtype where FO_Roomdet_Id=1";
            connection.Open();
            Reader = command.ExecuteReader();
            while (Reader.Read())
            {
                Image1.ImageUrl  = new MemoryStream((byte[])Reader.GetValue(0));                                    
            }
            connection.Close();

当我在 asp.net 中尝试这个时,它通过了一个错误。

错误 1 ​​无法将类型“System.IO.MemoryStream”隐式转换为 '字符串'

我该如何解决这个问题。并获取 mysql blob 图像只显示在 asp.net 图像控件中。

请帮帮我。

【问题讨论】:

    标签: asp.net mysql image blob


    【解决方案1】:

    您尝试做的事情没有意义:尝试显示您的图像的浏览器需要知道从哪里下载它。

    您应该设置一个特殊的 aspx 页面,专门用于图像生成,例如 GetImage.aspx。

    然后您的主页将具有指向此图像生成页面的 img html 标记:

    <img src="/GetImage.aspx?id=your_image_id"/>
    

    然后,在 GetImage.aspx 中,您根据其 id(从 URL 参数获取)从 DB 中检索图像。代码类似于:

    command = connection.CreateCommand();
            command.CommandText = "Select FO_Roomdet_Image from fo_roomtype where FO_Roomdet_Id=1"; // or dynamically fetch id with Request.QueryString and properly escape it
            connection.Open();
            Reader = command.ExecuteReader();
            while (Reader.Read())
            {
    
                Response.ContentType = "image/jpeg"; // if your image is a jpeg of course
                Response.BinaryWrite((byte[])Reader.GetValue(0));                                 
            }
            connection.Close();
    

    【讨论】:

    • Response.BinaryWrite(imageData); -> 什么是 imageData 先生?我在哪里可以找到它?
    • 对不起,我更新了我的答案以使其更清楚。是的,BinaryWrite 会写入您从数据库中检索到的 byte[]。
    • 不是一个正确的答案。请参阅我的答案。
    【解决方案2】:

    嗯,这绝对不是最简单的答案。 每次使用时,您不需要创建额外的 aspx 文件来生成和重新生成图像。

    您实际上可以通过它的字节数组将图像文件嵌入到 html 标记语言中。

    您需要做的就是从数据库中获取 BLOB 字节数组并使用它:

    <img src="data:image/png;base64,<%= System.Convert.ToBase64String((byte[])dr["img"])%>" />
    

    ...其中 dr 是从 DataSet 对象获得的 DataRow。

    我已经在 Internet Explorer 8 和所有现代浏览器中对其进行了测试,并且可以正常工作。

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 2015-06-22
      • 2018-09-19
      • 1970-01-01
      • 2011-01-29
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多