0

現在GenericHandler(.ashx)、画像コントロールで画像を表示するために使用しています。

以下のコードを参照してください。

[1] .ASPX
    <asp:Image ID="Image1" runat="server" Width="350px" Height="415px" />
[2] .cs(codebehind)
    Image1.ImageUrl = string.Format("GridviewImage.ashx?ItemID={0}", itemID);

次に、イメージをImage1バイト配列 ( byte[]) として取得する必要があります。

出来ますか?

4

1 に答える 1

1

ページに複数の画像をストリーミングする場合は、IsReusable を true に設定します。他のリンクのような DataSet ではなく、画像をストリーミングするために DataReader を使用します。

<img src='ImageHandler.ashx?ProductID=<%# Eval("ProductID")%>' alt="<%# Eval("ProductName") %>" 
    title="<%# Eval("ProductName") %>" />

    public class ImageHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["productID"] != null)
            {
                try
                {
                    string ProductID = context.Request.QueryString["ProductID"];
                    if (Convert.ToInt32(ProductID) > 0)
                    {
#if DEBUG
      const string CONN = "Initial Catalog=db1;Data Source=server1;Integrated Security=SSPI;";
#else
      const string CONN = "server=server2;database=db2;uid=un2;pwd=pw2;";
#endif
                        string selectQuery = "SELECT Photo FROM dbo.Products WHERE dbo.Products.ProductID=" + ProductID.ToString();
                        SqlConnection conn = new SqlConnection(CONN);
                        SqlCommand cmd = new SqlCommand(selectQuery, conn);

                        conn.Open();
                        SqlDataReader dr = cmd.ExecuteReader();

                        dr.Read();
                        context.Response.BinaryWrite((Byte[])dr[0]);
                        dr.Close();
                        conn.Dispose();
                        // context.Response.End(); --> caused an "Abort thread" error - this is correct and is a special exception
                    }
                }
                catch (Exception ex)
                {
                    ErrorReporting.LogError(ex);   
                }
            }
            else
                throw new ArgumentException("No ProductID parameter specified");
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
于 2012-10-11T04:38:21.813 に答える