Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Display image from database in ASP.net with C#

I know this kind of question has been asked many times. Yet I don't succeed in displaying an image from my db on a page.

I tried the following method.

    protected void Page_Load(object sender, EventArgs e)
    {
        //Get the picture id by url
        //Query here
        byte[] picture = queryoutput;
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(picture);
    }

Link to get the image.

<asp:Image runat="server" ImageUrl="~/givememypicture.aspx?pic=3" ID="testimage" />

The <asp:Image /> tag is located in between <asp:Content > tags.

When I run this code and check in firebug, it simply states 'Failed to load fiven URL'. I also tried putting the Respone.Con...(picture); part into a public method and call that method with the byte var given.

I'm quite new to asp.net, but I have somewhat more experience in c#. I start to really dislike asp.net... I have been struggling with this for about 20 hours already and tried a lot of options, yet none worked.

The best would be if I could just fill in the picture via the codefile from that same page. It seems quite illogical to me to call another page to load the image from.

Can somebody please tell me what I'm doing wrong here?

Solution: Master page reference removed from the page directive on the page that handles the image response. Also removed everything else except for the @page directive itself within the aspx file.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

try using a handler file (.ashx) put the code form your page_load in that lie so

public void ProcessRequest (HttpContext context) {
    //Get the picture id by url
        //Query here
        byte[] picture = queryoutput;
        Response.ContentType = "images/jpeg";
        Response.BinaryWrite(picture);
    }

    public bool IsReusable {
    get {
        return false;
    }
    }

then call the handler file and pass the correct querystring items from the

this should then work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...