在Image Web控件中设置二进制图像. [英] set binary image in Image web control.

查看:62
本文介绍了在Image Web控件中设置二进制图像.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我需要在图像Web控件中显示数据库中的二进制图像.我可以将二进制数据转换为图像,但是如何将其绑定到图像Web控件?

请帮助我.

In my web application, I need to display binary image from database in image web control.
I can convert binary data to image, but how to bind it to Image web control?

Please help me.

推荐答案

andiyuniar
我以前遇到过此问题.我处理它的方式是欺骗"内容页面以更改其ContentType以显示图像.我会通过查询字符串将其传递给id,然后将imageurl设置为欺骗页面.

下面的示例将为您提供帮助.请记住,我不知道这是否是最好的解决方案,但它对我有用.另外,如果通过此方法在页面上加载的图像过多,可能会降低性能.

创建WebForm ...我称为mineLoadImage.aspx
将此代码放入Load事件

Hi andiyuniar
I''ve had this issue before. The way I handled it was to "spoof" a content page to change it''s ContentType to display images. I would pass it an id via query string and then set the imageurl to the spoofed page.

Here''s an example that should help you. Please keep in mind, I don''t know if it''s the best solution, but it works for me. Also, there could be some performance hits if you have too many images being loaded on your page via this method.

Create a WebForm... i called mine LoadImage.aspx
Place this code in the Load event

<br />if (Request.QueryString["Id"] != null)<br />        {<br />            int Id = Convert.ToInt32(Request.QueryString["Id"].ToString());<br />            byte[] image = null;<br />            //You will have to add your connection information and grab the image from the database<br />            // ToDo: Load binary image into image variable;<br /><br />                if (image!=null)<br />                {<br />                    MemoryStream ms = new MemoryStream(image);<br />                    Bitmap b = new Bitmap(Image.FromStream(ms));<br /><br />                    Response.ContentType = "image/jpeg";<br />                    b.Save(Response.OutputStream, ImageFormat.Jpeg);<br />                }<br />                else<br />                {<br />                    Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));<br /><br />                    Response.ContentType = "image/jpeg";<br />                    b.Save(Response.OutputStream, ImageFormat.Jpeg);<br />                }<br />        }<br />        else<br />        {<br />            Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));<br /><br />            Response.ContentType = "image/jpeg";<br />            b.Save(Response.OutputStream, ImageFormat.Jpeg);<br />        }<br />



然后在要绑定图像的页面上,只需将ImageUrl属性设置为Webform加查询字符串即可.



Then on the page you want to bind your image to just set the ImageUrl property to the webform plus query string.

<br />  Image1.ImageUrl = "LoadImage.aspx?Id=1";<br />



快乐编码



Happy Coding


这篇关于在Image Web控件中设置二进制图像.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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