如何使用c#从数据库中检索多个图像

时间:2022-09-03 23:19:25

I have a database of 9 images, which keep changing, so I cannot directly set the src in html <img> tag to display the 9 images, I have to pick them from the database and bind them accordingly.

我有一个9张图片的数据库,一直在变化,所以我不能直接在html 如何使用c#从数据库中检索多个图像标签中设置src来显示9张图片,我必须从数据库中选择它们并进行相应的绑定。

I'm able to retrieve and print 1 image using Response.BinaryWrite(), but not all 9. My byte[] gets only the 1st image from the db,

我可以使用Response.BinaryWrite()检索并打印1映像,但不是全部9。我的byte[]只从db中获取第一个图像,

here's the code,

这里的代码,

            while (dr.Read())
            {
                Response.ContentType = "image/jpg";
                Response.BinaryWrite((byte[])(dr["fsImage"]));

            }

How do I retrieve all 9 images, and how do I bind them to either <asp:Image> tag or construct an html <img> tag dynamically

如何检索所有9个图像,如何将它们绑定到 标记或动态构建html 如何使用c#从数据库中检索多个图像标签?

I'm a novice, so please take it easy on me, if there are stupid mistakes ;)

我是个新手,如果我犯了愚蠢的错误,请放轻松。

Thanks in advance.

提前谢谢。

2 个解决方案

#1


2  

You can't have the web page give the content of all the images.

你不能让网页给出所有图片的内容。

In order to "dynamically" render the image you need to make a page which will give you a single image. This page would use code like what you have above.

为了“动态”渲染图像,您需要创建一个页面,该页面将为您提供单个图像。这个页面将使用与上面类似的代码。

Then to pick which image to show the url to the page would change for example

然后,选择显示该页面的url的图像将会发生变化。

http://youdomain/makeimage.aspx?imageid=1

and

http://youdomain/makeimage.aspx?imageid=2

The code in make image would then return the single image.

make映像中的代码将返回单个映像。

A note -- you will be much happier if you make the url

注意——如果你创建了url,你会更开心

http://youdomain/makeimage.aspx?imageid=1&mytype=.jpg

Thus having the image url end in the extension. Some browsers (IE) cheat and look at the end of the string to see what content type to expect. They will work with a url like above but not without the trialling .jpg.

因此,图像url在扩展中结束。一些浏览器(即)作弊并查看字符串的末尾,以查看预期的内容类型。他们将使用如上所述的url,但不需要测试。jpg。

Also, you will need to implement some form of caching our your performance will be a dog (esp if you try to scale.)

此外,您将需要实现某种形式的缓存,您的性能将会很差(特别是如果您尝试扩展的话)。

Good luck.

祝你好运。

#2


1  

Use HttpHandler and call in Img tag

使用HttpHandler并调用Img标记

one example

一个例子

<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public class Handler2 : IHttpHandler {


    public void ProcessRequest(HttpContext context)
    {

        if (context.Request.QueryString["pid"] != null)
        {
            string ID = context.Request.QueryString["pid"].ToString();

            if (dt.Rows.Count > 0)
            {
                int pid = Convert.ToInt32(ID);
                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
                myConnection.Open();
                //int i = Convert.ToInt32(context.Request.QueryString["id"]);
                string sql = "Select BackGroundImage from Wall_BackgrndImg_Master where FK_Company_Master=@ImageId";
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = pid;
                cmd.Prepare();
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                try
                {
                    context.Response.ContentType = "jpg";
                    context.Response.BinaryWrite((byte[])dr["BackGroundImage"]);
                    dr.Close();
                    myConnection.Close();
                }
                catch
                {

                }
            }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

and call like

和电话

    <img src="~/handlor.ashx?pid=1">
<img src="~/handlor.ashx?pid=2">
<img src="~/handlor.ashx?pid=3">
<img src="~/handlor.ashx?pid=4">
<img src="~/handlor.ashx?pid=5">

#1


2  

You can't have the web page give the content of all the images.

你不能让网页给出所有图片的内容。

In order to "dynamically" render the image you need to make a page which will give you a single image. This page would use code like what you have above.

为了“动态”渲染图像,您需要创建一个页面,该页面将为您提供单个图像。这个页面将使用与上面类似的代码。

Then to pick which image to show the url to the page would change for example

然后,选择显示该页面的url的图像将会发生变化。

http://youdomain/makeimage.aspx?imageid=1

and

http://youdomain/makeimage.aspx?imageid=2

The code in make image would then return the single image.

make映像中的代码将返回单个映像。

A note -- you will be much happier if you make the url

注意——如果你创建了url,你会更开心

http://youdomain/makeimage.aspx?imageid=1&mytype=.jpg

Thus having the image url end in the extension. Some browsers (IE) cheat and look at the end of the string to see what content type to expect. They will work with a url like above but not without the trialling .jpg.

因此,图像url在扩展中结束。一些浏览器(即)作弊并查看字符串的末尾,以查看预期的内容类型。他们将使用如上所述的url,但不需要测试。jpg。

Also, you will need to implement some form of caching our your performance will be a dog (esp if you try to scale.)

此外,您将需要实现某种形式的缓存,您的性能将会很差(特别是如果您尝试扩展的话)。

Good luck.

祝你好运。

#2


1  

Use HttpHandler and call in Img tag

使用HttpHandler并调用Img标记

one example

一个例子

<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public class Handler2 : IHttpHandler {


    public void ProcessRequest(HttpContext context)
    {

        if (context.Request.QueryString["pid"] != null)
        {
            string ID = context.Request.QueryString["pid"].ToString();

            if (dt.Rows.Count > 0)
            {
                int pid = Convert.ToInt32(ID);
                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
                myConnection.Open();
                //int i = Convert.ToInt32(context.Request.QueryString["id"]);
                string sql = "Select BackGroundImage from Wall_BackgrndImg_Master where FK_Company_Master=@ImageId";
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = pid;
                cmd.Prepare();
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                try
                {
                    context.Response.ContentType = "jpg";
                    context.Response.BinaryWrite((byte[])dr["BackGroundImage"]);
                    dr.Close();
                    myConnection.Close();
                }
                catch
                {

                }
            }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

and call like

和电话

    <img src="~/handlor.ashx?pid=1">
<img src="~/handlor.ashx?pid=2">
<img src="~/handlor.ashx?pid=3">
<img src="~/handlor.ashx?pid=4">
<img src="~/handlor.ashx?pid=5">