简体   繁体   English

如何使用c#从asp.net中的数据库绑定图像?

[英]how to bind image from database in asp.net using c#?

I have an image in a table(byte format). 我有一张表(字节格式)的图像。 I want to bind this image (byte format) to an image button.I search the record and I have to display this image to this image button.Someone please help me out.Any help I would be grateful to you.. 我想将此图像(字节格式)绑定到图像按钮。我搜索记录,并且必须将此图像显示到该图像按钮。请有人帮帮我。我将不胜感激。

Suppose that you have data in imageData byte array 假设您在imageData字节数组中有数据

byte[] imageData=...;

You need to convert it into base64 as follows 您需要将其转换为base64 ,如下所示

string imageBase64 = Convert.ToBase64String(imageData);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);

An in your page you can use 您可以在页面中使用

<img src= "<%= ImageSrcBytes %>" />

Note that it will not work in lower end browser. 请注意,它不适用于低端浏览器。

For those browsers you need to save image in local folder and then provide the url for that image. 对于这些浏览器,您需要将图像保存在本地文件夹中,然后提供该图像的URL。

Or you can create a handler and call that handler as image source. 或者,您可以创建一个处理程序并将该处理程序称为图像源。

Some useful links 一些有用的链接

  1. Displaying Database Images in Gridview with HttpHandler 使用HttpHandler在Gridview中显示数据库图像

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

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