从数据库中检索并显示在ASP.NET图像而没有的HttpHandler [英] Retrieve image from database and display on ASP.NET without httphandler

查看:198
本文介绍了从数据库中检索并显示在ASP.NET图像而没有的HttpHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET网站,我想进入EmployeeName,并上传EmployeePhoto,然后检索雇员信息,并在网站上显示Employeephoto。

我已经创建了一个SQL表EmployeePhoto - 型形象

我用这个code对插入的照片(正常工作)
http://i.stack.imgur.com/qmphk.png

然而,当我想我的SQL插入图片加载到网站上,我得到这个InputStream中不存在错误。

载照片codeS&安培;误差 http://i.stack.imgur.com/X2okW.png

我不希望使用的HttpHandler的解决方案

编辑:我没有正确的答案尚未:(

非常感谢你。

  cnn.Open();
            CMD的SqlCommand =新的SqlCommand(SELECT EmployeeFirstName,EmployeeLastName,EmployeePhoto FROM雇员WHERE雇员= @myvalue,美国有线电视新闻网)
            cmd.Parameters.AddWithValue(@ myvalue的,(ListBox1.SelectedValue));
            SqlDataReader的博士= cmd.ExecuteReader();
            如果(dr.HasRows)
            {
                而(dr.Read())
                {
                    TextBox1.Text = dr.GetString(0);
                    TextBox2.Text = dr.GetString(1);
                    字节[]为imageData =(字节[])博士[2];
                    InputStream.Read(的imageData,0,(字节[])博士[2]);
                    Image1的图像= Image.FromStream(为imageData);                }
            }
            cnn.Close();


解决方案

替换

 字节[]为imageData =(字节[])博士[2];
                InputStream.Read(的imageData,0,(字节[])博士[2]);
                Image1的图像= Image.FromStream(为imageData);

通过

 字节[]为imageData =(字节[])博士[2];
                Image1的图像= Image.FromStream(新的MemoryStream(为imageData));

它看起来像发生了误差,因为它无法找到的InputStream 对象所使用。通过创建一个新的MemoryStream对象绕过这一点。

另外你正在传递一个二进制数组 Image.FromStream ,我不认为会被正确地接受。

此外使用图片对象可能不会帮助你完成这个解决方案,因为它是不一样的图片 Web控件。

详情参见下面的:

为System.Drawing.Image

System.Web.UI.Controls。图片

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) http://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 http://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();

解决方案

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

这篇关于从数据库中检索并显示在ASP.NET图像而没有的HttpHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆