Nate Pet
Nate Pet

Reputation: 46222

asp.net image how to assign value to IMG src

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.

Upvotes: 0

Views: 2033

Answers (3)

maple_shaft
maple_shaft

Reputation: 10463

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.

Upvotes: 1

Leon
Leon

Reputation: 3401

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.

Upvotes: 1

Oded
Oded

Reputation: 498904

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.

Upvotes: 3

Related Questions