3

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.

Mixxiphoid
  • 1,044
  • 6
  • 26
  • 46

4 Answers4

6

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

stack72
  • 8,198
  • 1
  • 31
  • 35
  • This is a very good idea but it won't solve his question. If he can't do it from the page class, the same code in a handler wont work either. First he should fix it in his page load code, then move it into a handler like you suggest for better performance. – DavidJBerman Aug 08 '11 at 19:29
  • can you post your entire method that does the work and then we can help – stack72 Aug 08 '11 at 20:01
  • Got it working now, had to change a few little things. Thanks a lot! Finally getting some data now, thanks again! – Mixxiphoid Aug 08 '11 at 20:10
5

There's an error in your path reference...

ImageUrl="/~givememypicture.aspx?pic=3"

should be:

ImageUrl="~/givememypicture.aspx?pic=3"

~ is shorthand for "the application root" and needs to be followed by a slash to indicate that it's a directory. Think of it as similar to other path shorthand notations such as . and ...

David
  • 208,112
  • 36
  • 198
  • 279
  • @Mixxiphoid: In that case, I'll echo Icarus' question. What happens when you go to the URL directly? What response does the server send you? If you step through the server-side code on that request, where does it differ from what you expect? – David Aug 08 '11 at 19:41
  • check my comment at Icarus' question. – Mixxiphoid Aug 08 '11 at 19:44
  • 1
    @Mixxiphoid: Does `givememypicture.aspx` have any other content? The `.aspx` file for that should have nothing in it besides the `@Page` directive, and it should not reference a Master Page. If there's any content in there, you'll want to delete it. It's just polluting the response content, and all you want for response content is binary image data. – David Aug 08 '11 at 19:46
  • I removed everything except for the @page directive. Firebug now throws me an error that the image is corrupt :). Finally I'm receiving something. Not there yet, but thanks a lot! – Mixxiphoid Aug 08 '11 at 19:59
0

Make sure you call Response.Flush(); and also Response.End(); and see if that does the trick

Also, your content type has a misspelling. Is not "images/jpgeg" I think it's "image/jpeg"

EDIT: Yes, I just confirmed in Wikipedia that the correct content-type is image/jpeg.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • @Mixxiphoid If you go directly to the URL, do you get anything? in other words, if you go to http://localhost/application_path/givememypicture.aspx?pic=3 does it throw an exception, does it show a blank page, what happens? – Icarus Aug 08 '11 at 19:33
  • I got the error: Content controls have to be top-level controls in a content page or a nested master page that references a master page. – Mixxiphoid Aug 08 '11 at 19:42
  • 1
    @mixxiphoid read this thread: http://stackoverflow.com/questions/835877/master-page-weirdness-content-controls-have-to-be-top-level-controls-in-a-cont maybe can help – Icarus Aug 08 '11 at 20:02
0

I do this all the time. Here's some sample code:

Response.ContentType = "image/jpeg"; Response.BinaryWrite(bytes);

That's all there is too it. If it's not working, then something is probably wrong with your data.

A flush is not required. I have working code for this open on my screen right now.

I would suggest that you try writing that buffer of data to a file and see if it opens up as a valid picture. I bet something's wrong with that data.

Also, it's a good idea to enable browser side caching for your dynamic content. Here's a GREAT link that shows exactly how to do that, which will boost your performance / scalability a lot.

http://edn.embarcadero.com/article/38123

DavidJBerman
  • 945
  • 1
  • 10
  • 17