0

I have an ASP.NET website where I want to enter EmployeeName , and upload EmployeePhoto, then retrieve Employee Info and display Employeephoto on website.

I have created an SQL table with EmployeePhoto - type "image".

I use this code to insert photo on (it works fine) https://i.stack.imgur.com/qmphk.png

However when I want to load my inserted photo in SQL into website, I get this Inputstream does not exist error.

load photo codes & error https://i.stack.imgur.com/X2okW.png

I don't want to use the httphandler solution

EDIT: I have no correct answer yet :(

Thank you very much.

            cnn.Open();
            SqlCommand cmd = new SqlCommand("SELECT EmployeeFirstName,EmployeeLastName,EmployeePhoto FROM Employees WHERE EmployeeID = @myvalue", cnn);
            cmd.Parameters.AddWithValue("@myvalue", (ListBox1.SelectedValue));
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    TextBox1.Text = dr.GetString(0);
                    TextBox2.Text = dr.GetString(1);


                    byte[] imagedata = (byte[])dr[2];
                    InputStream.Read(imagedata, 0, (byte[])dr[2]);
                    Image Image1 = Image.FromStream(imagedata);

                }
            }
            cnn.Close();
  • **"I don't want to use the httphandler solution"**: why? – Steve B Oct 02 '13 at 15:38
  • You want to display a picture stored in the DB? the handler is the only solution. And I think this is a duplicate, as the requirement is the same. – Steve B Oct 02 '13 at 15:45
  • check your `using`. You will have to learn that there are more than one `Image` class, as @Nunners said in its answer below – Steve B Oct 02 '13 at 16:10
  • @SteveB Sorry not understood what you mean. How can I fix error in Nunners answer? http://n1310.hizliresim.com/1g/2/t6tk5.png ? – İsmail Türütçü Oct 02 '13 at 16:14
  • Read about .Net namespaces. System.Drawing.Image is not the same as System.Web.UI.Controls.Image – Steve B Oct 02 '13 at 16:31

4 Answers4

2

Replace

                byte[] imagedata = (byte[])dr[2];
                InputStream.Read(imagedata, 0, (byte[])dr[2]);
                Image Image1 = Image.FromStream(imagedata);

With

                byte[] imagedata = (byte[])dr[2];
                Image Image1 = Image.FromStream(new MemoryStream(imagedata));

It looks like the error is occurring because it cannot find the InputStream object you are using. Bypass this by creating a new MemoryStream object.

Also you are passing a binary array into Image.FromStream which I don't think will be correctly accepted.

Also using the Image object will probably not help you with this solution as it is not the same as the Image web control.

See below for details :

System.Drawing.Image

System.Web.UI.Controls.Image

Nunners
  • 3,047
  • 13
  • 17
1

With the way HTML was designed, you should use some sort of handler since the img tag in HTML refers to an external resource (e.g. src tag). For the longest time, there was not a way to include the image in the HTML that is returned from the server.

That being said, things have changed, and it is possible now. You can base-64 encode your image and include it in the markup itself using a specially formatted src attribute:

<img src="data:<MIMETYPE>;base64,<BASE64_ENCODED_IMAGE>">

Not all browsers support this, so be careful.

Also, one big downside to this approach is that the image isn't cached by the browser, so each time this page is requested there will be extra overhead from the server to produce this Base64 value, whereas if you had a URL, the browser could cache the image based on the URL and not have to get it from the server on every request.

To get the Base64 string, use the static method System.Convert.ToBase64String(byte[]).

Brian Ball
  • 12,268
  • 3
  • 40
  • 51
-1

That's because there is no object called InputStream.

Try this instead:

byte[] imagedata = (byte[])dr[2];
Image Image1 = Image.FromStream(new MemoryStream(imagedata));

That will put the byte[] into a memory stream, and then allow you to use the Image.FromStream();

TheDaveJay
  • 753
  • 6
  • 11
  • I get this error http://n1310.hizliresim.com/1g/2/t6tk5.png – İsmail Türütçü Oct 02 '13 at 15:52
  • The op's wants to display the picture on the web. This code do not show how. Either update your code to fulfill the requirement, or explain the OP's why he requires an handler. – Steve B Oct 02 '13 at 16:10
  • Looking at his code, it was very unclear as to what image object he was using. I was referencing the bitmap object, as his code had this: `Image Image1 = Image.FromStream(imagedata);` What he is asking for then is already covered [here](http://stackoverflow.com/questions/1738020/bytearray-to-image-asp-net) – TheDaveJay Oct 03 '13 at 09:16
-1

I believe this might work:

byte[] bitMapData = dr[2] as byte[] ?? null;

        if (bitMapData != null)
        {
            using (MemoryStream stream = new MemoryStream(bitMapData))
            {
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream);
            }
        }
Kevin
  • 2,566
  • 1
  • 11
  • 12
  • Photos do not appear on website :( – İsmail Türütçü Oct 02 '13 at 15:54
  • Also, , so "Image1" does not exist anywhere in your codes, so how can it be displayed there anyway? – İsmail Türütçü Oct 02 '13 at 15:57
  • The op's wants to display the picture on the web. This code do not show how. Either update your code to fulfill the requirement, or explain the OP's why he requires an handler. – Steve B Oct 02 '13 at 16:09
  • You can't assign a bitmap directly to an asp.net image control. There are several ways you can get around this (writing the image to disk and setting the src, or creating a page that returns data via Response.BinaryWrite(...) and setting the src to the page) – Kevin Oct 02 '13 at 16:18