Populate image from db

  • Thread starter Thread starter DavidC
  • Start date Start date
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.
 
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 filepath.
 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
 
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
 
Thanks. Now I just have to convert your C# to VB.
--
David


Alexey Smirnov 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
.
 
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!
 
Back
Top