C# 从数据库中检索图像并在没有 httphandler 的情况下在 ASP.NET 上显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19140729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:13:38  来源:igfitidea点击:

Retrieve image from database and display on ASP.NET without httphandler

c#asp.netsql

提问by ?smail Türüt?ü

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

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

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

我用 EmployeePhoto 创建了一个 SQL 表 - 输入“图像”。

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

我使用此代码插入照片(它工作正常) 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.

但是,当我想将 SQL 中插入的照片加载到网站时,我收到此 Inputstream 不存在错误。

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

加载照片代码和错误http://i.stack.imgur.com/X2okW.png

I don't want to use the httphandler solution

我不想使用 httphandler 解决方案

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

回答by Nunners

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 InputStreamobject you are using. Bypass this by creating a new MemoryStream object.

看起来错误正在发生,因为它找不到InputStream您正在使用的对象。通过创建一个新的 MemoryStream 对象来绕过这个。

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

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

Also using the Imageobject will probably not help you with this solution as it is not the same as the Imageweb control.

同样使用该Image对象可能无法帮助您解决此解决方案,因为它与ImageWeb 控件不同。

See below for details :

详情见下文:

System.Drawing.Image

系统.绘图.图像

System.Web.UI.Controls.Image

System.Web.UI.Controls.Image

回答by TheDaveJay

That's because there is no object called InputStream.

那是因为没有名为 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();

这会将它放入byte[]内存流中,然后允许您使用 Image.FromStream();

回答by Kevin

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

回答by Brian Ball

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

按照 HTML 的设计方式,您应该使用某种处理程序,因为imgHTML 中的标记指的是外部资源(例如src标记)。很长一段时间以来,没有办法将图像包含在从服务器返回的 HTML 中。

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 srcattribute:

话虽如此,事情已经发生了变化,现在是可能的。您可以使用特殊格式的src属性对图像进行 base-64 编码并将其包含在标记本身中:

<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.

此外,这种方法的一大缺点是浏览器不会缓存图像,因此每次请求此页面时,服务器都会产生额外的开销来生成此 Base64 值,而如果您有 URL,则浏览器可以根据 URL 缓存图像,而不必在每次请求时从服务器获取它。

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

要获取 Base64 字符串,请使用静态方法System.Convert.ToBase64String(byte[])