How to display mysql blob image in asp.net image c

2019-01-27 04:46发布

问题:

I known the way to display the mysql blob image in Windows Forms.

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);
            }

But now i doing a asp.net project. In this image not have the image property,.

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();

When i try this in asp.net, it through an error.

Error 1 Cannot implicitly convert type 'System.IO.MemoryStream' to 'string'

How Can i Solve this issue. and get mysql blob image just display in asp.net image control.

help me please.

回答1:

What you're trying to do doesn't make sense: the browser trying to display your image will need to know where to download it from.

You should setup a special aspx page, dedicated to image generation, for example GetImage.aspx.

Your main page will then have img html tags pointing to this image generation page:

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

Then, inside GetImage.aspx, you retrieve the image from DB according to its id (fetched from URL parameter). The code would be something like:

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();


回答2:

Well, this is definetely not the simplest answer. You do not need to create an extra aspx file to image generation and re-generation each time when it's used.

You can actually embed the image files into the html markup language by it's byte array.

All you need to do is get the BLOB byte array from your database and use this:

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

...where dr is a DataRow obtained from a DataSet object.

I have tested it in Internet Explorer 8 and all modern browsers and it works.