Display image from database in ASP.net using C #

I know this question has been asked many times. However, I am unable to display the image from my db on the 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 /> is between the <asp:Content > tags.

When I run this code and check firebug, it simply indicates: β€œFailed to load fiven-url”. I also tried to put the Respone.Con...(picture); part Respone.Con...(picture); into the public method and call this method with the specified byte var.

I am new to asp.net, but I have some more experience in C #. I start to dislike asp.net a lot ... I struggled with this for about 20 hours and tried many options, but no one worked.

It would be best if I could just fill out the image through code from the same page. It seems completely illogical to me to call another page to load the image.

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

Solution: The link to the main page is removed from the page directive on the page that processes the image response. Everything else has been removed, except for the @page directive in the aspx file itself.

+2
database image
Aug 08 2018-11-18T00:
source share
4 answers

try using a handler file (.ashx), paste the page_load form code into it, 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 request elements from

it should work

+6
Aug 08 2018-11-11T00:
source share

There is an error in your path link ...

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

it should be:

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

~ is an abbreviation for "application root" and is followed by a slash indicating that it is a directory. Think about how it looks like other line abbreviations, such as . and ..

+5
Aug 08 2018-11-21T00:
source share

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

In addition, your content type has spelling. Isn't that "images / jpgeg" I think it's "image / jpeg"

EDIT: Yes, I just confirmed on Wikipedia that the right type of content is image / jpeg,

0
Aug 08 2018-11-11T00:
source share

I do this all the time. Here is a sample code:

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

That is all there is. If it does not work, then there is probably something wrong with your data.

Flash is not required. I have a working code for this discovery on my screen right now.

I would suggest that you try to write this data buffer to a file and see if it opens as a real image. I bet something is wrong with this data.

It is also recommended that you enable browser-side caching for your dynamic content. There's a BIG link here that shows how to do this, which will greatly increase your performance / scalability.

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

0
Aug 08 '11 at 19:34
source share



All Articles