Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

c# - Rendering bytes from sql server to an image control?

Markup:

<asp:ListView ID="lvGallery" runat="server" DataSourceID="SqlDataSource1">
    <LayoutTemplate>
        <table runat="server" id="tableGallery">
            <tr runat="server" id="itemPlaceHolder"></tr>       
        </table>       
    </LayoutTemplate>
    <ItemTemplate>       
    <tr>
        <td>
            <div class="box_img2">
                <div class="g_size">
                    <a runat="server" id="linkImage">
                        <img id="Image1" runat="server" src="MyImage.jpg" />
                    </a>
                </div>         
            </div>
        </td>
    </tr>
   </ItemTemplate>
</asp:ListView>    
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..."  ProviderName="System.Data.SqlClient"  SelectCommand="SELECT [Image] FROM [GalleryImages]"></asp:SqlDataSource>

Code-behind:

public void ProcessRequest(HttpContext context)
{
    //write your handler implementation here.
    string username = Convert.ToString(context.Request.QueryString["username"]);
    if (username != null)
    {
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();
        byte[] arrContent;
        DataRow dr;
        string strSql;
        strSql = "Select Image from GalleryImages where username = '" + username + "'";
        da = new SqlDataAdapter(strSql, connection.ConnectionString);
        da.Fill(ds);
        dr = ds.Tables[0].Rows[0];
        arrContent = (byte[])dr["ImageFile"];
        context.Response.ContentType = "jpeg";
        context.Response.OutputStream.Write(arrContent, 0, arrContent.Length);
        context.Response.End();
    }
}

I have also added httphandlers section in web.config. But I don't get the images in ListView control.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to create an http handler that returns the image

public void ProcessRequest(HttpContext context)
{
    byte[] yourImage = //get your image byte array
    context.Response.BinaryWrite(yourImage);
    context.Request.ContentType = "image/jpeg";
    context.Response.AddHeader("Content-Type", "image/jpeg");
    context.Response.AddHeader("Content-Length", (yourImage).LongLength.ToString());
    con.Close();

    context.Response.End();
    context.Response.Close();
}

You can can do that by creating a GenericHandler file type from the visual studio and add the previous code in then you can call you can write the url of the generic handler as the image source


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...