简体   繁体   English

如何在asp.net(C#Web)中使用图像控制

[英]How to use Image conrtrol in asp.net (C# web)

I have an image stored in my MS Access database with the data type OLE Object. 我在MS Access数据库中存储了一个图像,数据类型为OLE Object。 I want it to display in an Image control. 我希望它显示在Image控件中。 How can I do this? 我怎样才能做到这一点? I tried this, but only in a PictureBox control in windows forms. 我试过了,但是只能在Windows窗体的PictureBox控件中进行。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

You need to create a page that can read the image from the database as binary data, then write that data directly to http response as binary data using Response.BinaryWrite to feed the image out. 您需要创建一个页面,该页面可以作为二进制数据从数据库读取图像,然后使用Response.BinaryWrite将数据作为二进制数据直接写入http响应,以提供图像。 Then the src attribute of your image is pointed at the page itself like: 然后,图像的src属性指向页面本身,如下所示:

<img src="image.aspx" />

And the code behind image.aspx: 以及image.aspx背后的代码:

// An assumed method to get binary data our of the database  
var bytes = YourDataLayer.GetBinaryImageData();  

Response.Clear();   
Response.AddHeader("Content-Disposition","attachment;filename=filename.jpg");   
Response.ContentType = @"image\jpg";   
Response.BinaryWrite(bytes);   
Response.End();  

从数据库中读取图像作为字节数组,然后创建一个临时图像对象,并将其分配给页面上的webcontrol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM