Populate image from db

D

DavidC

I have small images stored in a SQL table as varbinary(max). How do I load
that stored image into an asp.net Image control...or should I use something
different? I only found an ImageUrl property but that refers to a file path.
Any help is appreciated. Thanks.
 
G

Guest

I have small images stored in a SQL table as varbinary(max).  How do I load
that stored image into an asp.net Image control...or should I use something
different?  I only found an ImageUrl property but that refers to a file path.
 Any help is appreciated.  Thanks.

Use ASHX generic handler

<img src="image.ashx">

Let me know if I should help with the code for ashx
 
G

Guest

Yes, please. I am not familiar with ashx. Thanks.
--
David








- Show quoted text -

it must be something like this

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
GetFromDb();
}

private void GetFromDb()
{

..... your database code here ....

byte[] content = (byte[])db.ExecuteScalar(sql);

HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.BinaryWrite(content);

}

public bool IsReusable
{
get
{
return false;
}
}

}

Add new ashx to your project and copy the code into it.

When it works, you will be able to get an image as http://yoursite/image.ashx
 
D

DavidC

Thanks. Now I just have to convert your C# to VB.
--
David


Anon User said:
Yes, please. I am not familiar with ashx. Thanks.
--
David








- Show quoted text -

it must be something like this

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
GetFromDb();
}

private void GetFromDb()
{

..... your database code here ....

byte[] content = (byte[])db.ExecuteScalar(sql);

HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.BinaryWrite(content);

}

public bool IsReusable
{
get
{
return false;
}
}

}

Add new ashx to your project and copy the code into it.

When it works, you will be able to get an image as http://yoursite/image.ashx
.
 
G

Guest

Thanks. Now I just have to convert your C# to VB.
--
David



it must be something like this
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
GetFromDb();
}
private void GetFromDb()
{
..... your database code here ....
byte[] content = (byte[])db.ExecuteScalar(sql);
HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.BinaryWrite(content);

public bool IsReusable
{
get
{
return false;
}
}

Add new ashx to your project and copy the code into it.
When it works, you will be able to get an image ashttp://yoursite/image..ashx
.- Hide quoted text -

- Show quoted text -

Here's more on VB :)
http://msdn.microsoft.com/en-us/library/ms228090.aspx

Have fun!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top