0

I have the following code that is hard coded

    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUh .... " >

Through asp.net how do I assign the value of src as I need to pull the value from the db and assign it a value vs. the hard coded way that I currently have it as. I am using win form.

Note that src has a value of string.

3
  • How is the image stored in the database? Commented May 11, 2012 at 19:04
  • Is it base64 encoded already? Commented May 11, 2012 at 19:06
  • 1
    Can you please explain exactly which part of the process you are having difficulties with? It is not clear from the question. Commented May 11, 2012 at 19:07

3 Answers 3

3

If you want to embed the image data as as data URI as in your example, you will need to get the raw image data from the database (if not already base64 encoded) and base64 encode it using Convert.ToBase64String.

You can assign such a value directly to the image src, appending data:image/png;base64, if not already there.

If, however, you don't want to use a data URI, the way most people do this is by creating an HTTP handler that will pick up an image from the database and return the byte array and pointing the src attribute to it. There are multiple questions to that effect on this site - see these search results.

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

Comments

1

You can always change the HTTP response to the PNG content type, feed the binary data of the image into the response body, and serve that response from ASP.

You just need to pass a unique identifier for the image you which to show as a request parameter.

The src attribute can then point to the URL of this ASP page with the request parameter representing the picture you want to load.

Comments

1

You can use <asp:Image Runat="server".../> control, or you can give an ID and make the Img be server-size.

Ex: <img src="..." runat="server" ID="MyImageUniqueID" />

You can then access the properties of this control on the server side and populate them from DB.

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.