How to Show a Image in Database in the Image Control of ASP.NET

How to show a image in database in the image control of Asp.net?

You can create an HttpHandler(ashx) page which would take a querystring and set that as the imageUrl property of the image control

<asp:image id="imgEmployee" imageUrl="DisplayImage.ashx?employeeId=<someId>"/>

Now in DisplayImage.ashx, you can override the Processrequest like below:-

    public void ProcessRequest (HttpContext context) 
{
int employeeId;
if (context.Request.QueryString["employeeId"] != null)
employeeId = Convert.ToInt32(context.Request.QueryString["employeeId"]);
else
throw new ArgumentException("No parameter specified");

byte[] imageData= ;// get the image data from the database using the employeeId Querystring
Response.ContentType = "image/jpeg"; // You can retrieve this also from the database
Response.BinaryWrite(imageData);

}

Web.config changes:-

<httpHandlers>
<add verb="*" path="img/*" type="DisplayImage"/>
</httpHandlers>

Details here and here.

Hope this helps..

how to show an image in image control of which image path is stored in database when user selects a value in dropdownlist in asp.net?

Providing the 'pic' column in database holds links to the images are absolute, it also depends on how you have set the drop down list up.

You will need an OnSelectedIndexChanged for the drop down which does the following:

Selects the pic url from the database where subcategory = dropdown value or text. (this depends on how you set the drop-down list up).

With that obtained value set the pic to the one you have obtained.

You need the following, providing code would make it easier.

In you ASP file add the following to the dropdown:

OnSelectedIndexChanged="drp_SelectedIndexChanged" AutoPostBack="True"

Within code behind you need:

   protected void drp_SelectedIndexChanged(object sender, EventArgs e) {
//SQL To obtain url for image based on drop down selection
//providing code makes this easier
//with return url:
Image1.ImageUrl = //returned url
}

how to show image from database in asp:image with linq?

The ASP.NET Image control loosely represents an <img> tag in HTML. As a result you can only get an image into an HTML doocument by setting the URL to the image content you want to embed in the page.

<img src="images/picture.png" />

This means that you need a mechanism to take an HTTP request asking for an image resource, and return a response containing the image binary data.

With ASP.NET Web API this becomes a trivial operation to implement:

public HttpResponseMessage GetImage(string username)
{
DataClassesDataContext db = new DataClassesDataContext();
usertable thisuser = db.usertables.FirstOrDefault(
p => p.username == username);

if (thisuser == null)
{
return new HttpResponseMessage(HttpStatusCode.NotFound));
}

// Create a stream to return to the user.
var stream = new MemoryStream(thisuser.picture.ToArray());

// Compose a response containing the image and return to the user.
var result = new HttpResponseMessage();

result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("image/jpeg");

return result;
}

If you can't use Web API, you'll have to implement an HTTP Handler to do the same job.

In your ASP.NET page, you will have to set the property ImageUrl to the address configured for your controller/handler, including the username as part of the URL.

How to load image in image control of ASP.NET from database?

check how the path is resolved by your aspx page based on the application structure (your application folders structure)

Show image from Sql Server in a asp image control

Here's a solution from a similar, recent question:

Display image from database in ASP.net with C#

You need to use a HttpHandler class to retrieve and write the stream.

How to display mysql blob image in asp.net image control?

What you're trying to do doesn't make sense: the browser trying to display your image will need to know where to download it from.

You should setup a special aspx page, dedicated to image generation, for example GetImage.aspx.

Your main page will then have img html tags pointing to this image generation page:

<img src="/GetImage.aspx?id=your_image_id"/>

Then, inside GetImage.aspx, you retrieve the image from DB according to its id (fetched from URL parameter). The code would be something like:

command = connection.CreateCommand();
command.CommandText = "Select FO_Roomdet_Image from fo_roomtype where FO_Roomdet_Id=1"; // or dynamically fetch id with Request.QueryString and properly escape it
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{

Response.ContentType = "image/jpeg"; // if your image is a jpeg of course
Response.BinaryWrite((byte[])Reader.GetValue(0));
}
connection.Close();


Related Topics



Leave a reply



Submit