0

How would I go about displaying images stored in varbinary(max) on a webpage? I have looked around and a lot of people recommend using the Image server control, but the images I am displaying are variable in number, so I'm not sure how I could use the control?

I also need to be able to resize and check the extension.

1
  • How are you accessing the database? Commented Dec 13, 2011 at 19:23

4 Answers 4

1

Here is a good link that should get you started with what you are trying to achieve.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Nice link to a fully documented article showing how to do it step-by-step.
1

Typically, you would make a page that would directly write the bytes from your DB to the response stream and then set the response content type to the appropriate image format. Example:

Response.Write(myDbByteArray);
Response.ContentType = "image/png";
Response.End();

Then you would simply point images (even basic tags) to your url:

<img src="Path/ToMyPage.aspx?imgId=1" alt="Some Image" />

As long as you return the right content type, it doesnt matter that the image tag source is an aspx page.

Comments

1

Tejs give you the gist of it (ie. write the image into the response stream), but it left the interesting tidbits out. this article Download and Upload images from SQL Server via ASP.Net MVC shows how to transfer images to and from SQL Server using efficient streaming semantics and no intermediate file. You should try to avoid getting the image into a byte[] as most solutions naively suggest, because it leads to tremendous memory consumption and does not scale.

Comments

0

I'm using a generic handler for images (ashx) and in that handler I use response.binarywrite

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.