且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何绑定在asp.net中的SQL数据表的图片库?

更新时间:2023-02-16 17:01:42

您需要使用上写上你的回应二进制值

HttpContext.Current.Response.BinaryWrite()

首先你需要设置一个图像的ImageUrl 属性元素为空 ASPX 页面或 ASHX 处理程序,作为一个更好的选择。

,然后发送 ID 其二进制值查询字符串到该页面,像这样的记录:

 < ASP:图片ID =Image1的
    =服务器
    的CssClass =w120px h120px
    的ImageUrl ='ImageHandler.ashx imageID = 1?/>

和特定网页的后面,然后在code (ashx的处理程序,在我们的例子)您需要从得到的二进制值的质数据库和写入这样的回应:

 公共类ImageHandler:IHttpHandler的
{    公共无效的ProcessRequest(HttpContext的背景下)
    {
        字符串ID = HttpContext.Current.Request.QueryString [imageID];
        VAR图像= GetBinaryImageFromDataBaseByRecordID(ID);
        context.Response.ContentType =图像/ JPEG; //你需要将其设置为内容类型图像的
        context.Response.BinaryWrite(图片);
    }    私人字节[] GetBinaryImageFromDataBaseByRecordID(字符串ImageRecordID)
    {
        抛出新的异常(); //你需要从数据库使用ImageRecordID并返回得到的二进制值;
    }    公共BOOL IsReusable
    {
        得到
        {
            返回false;
        }
    }
}

如果你有一个主题一个以上的图像,例如3张图片,你可以使用3 图片元素,他们要求像这样存在的处理程序:

 < ASP:图片ID =Image1的
    =服务器
    的CssClass =w120px h120px
    的ImageUrl ='ImageHandler.ashx imageID = 1?/>< ASP:图片ID =IMAGE2
    =服务器
    的CssClass =w120px h120px
    的ImageUrl ='ImageHandler.ashx imageID = 20?/>< ASP:图片ID =图像3
    =服务器
    的CssClass =w120px h120px
    的ImageUrl ='ImageHandler.ashx imageID = 30?/>

I have binary data in SQL database of images.And there may be multiple records of any single ID.Means there may be multiple images for one ID.

And i have to fetch binary data of images from database and convert into image and bind with image gallery.

I am working on ASP.NET 4.0.

I want to do that without any third party control.

Can any one help me out on this ?

you need to write your binary value on response using

HttpContext.Current.Response.BinaryWrite()

first you need to set the ImageUrl attribute of an image element to a empty aspx page, or an ashx handler, as a better choice.

and then send the id of record which has the binary value as querystring to that page, like this:

<asp:Image ID="Image1" 
    runat="server" 
    CssClass="w120px h120px" 
    ImageUrl='ImageHandler.ashx?imageID=1' />

and then in code behind of that specific page (ashx handler, in our case) you need to get binary value from your dbase and write to response like this:

public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string id = HttpContext.Current.Request.QueryString["imageID"];
        var image = GetBinaryImageFromDataBaseByRecordID(id);
        context.Response.ContentType = "image/jpeg"; // you need to set this to content-type of your image
        context.Response.BinaryWrite(image);
    }

    private byte[] GetBinaryImageFromDataBaseByRecordID(string ImageRecordID)
    {
        throw new Exception(); // you need to get binary value from db using ImageRecordID and return;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

if you have more than one image for a subject, for example 3 images, you can use 3 image element, and they request to existed handler like this:

<asp:Image ID="Image1" 
    runat="server" 
    CssClass="w120px h120px" 
    ImageUrl='ImageHandler.ashx?imageID=1' />

<asp:Image ID="Image2" 
    runat="server" 
    CssClass="w120px h120px" 
    ImageUrl='ImageHandler.ashx?imageID=20' />

<asp:Image ID="Image3" 
    runat="server" 
    CssClass="w120px h120px" 
    ImageUrl='ImageHandler.ashx?imageID=30' />