Populate image from db

Discussion in 'ASP .Net' started by DavidC, Feb 3, 2010.

  1. DavidC

    DavidC 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.
    --
    David
    DavidC, Feb 3, 2010
    #1
    1. Advertising

  2. On Feb 3, 11:26 pm, DavidC <> wrote:
    > 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.
    > --
    > David


    Use ASHX generic handler

    <img src="image.ashx">

    Let me know if I should help with the code for ashx
    Alexey Smirnov, Feb 3, 2010
    #2
    1. Advertising

  3. DavidC

    DavidC Guest

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


    "Alexey Smirnov" wrote:

    > On Feb 3, 11:26 pm, DavidC <> wrote:
    > > 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.
    > > --
    > > David

    >
    > Use ASHX generic handler
    >
    > <img src="image.ashx">
    >
    > Let me know if I should help with the code for ashx
    > .
    >
    DavidC, Feb 4, 2010
    #3
  4. On Feb 4, 2:46 pm, DavidC <> wrote:
    > Yes, please. I am not familiar with ashx. Thanks.
    > --
    > David
    >
    >
    >
    > "Alexey Smirnov" wrote:
    > > On Feb 3, 11:26 pm, DavidC <> wrote:
    > > > 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.
    > > > --
    > > > David

    >
    > > Use ASHX generic handler

    >
    > > <img src="image.ashx">

    >
    > > Let me know if I should help with the code for ashx
    > > .- Hide quoted text -

    >
    > - 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
    Alexey Smirnov, Feb 4, 2010
    #4
  5. DavidC

    DavidC Guest

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


    "Alexey Smirnov" wrote:

    > On Feb 4, 2:46 pm, DavidC <> wrote:
    > > Yes, please. I am not familiar with ashx. Thanks.
    > > --
    > > David
    > >
    > >
    > >
    > > "Alexey Smirnov" wrote:
    > > > On Feb 3, 11:26 pm, DavidC <> wrote:
    > > > > 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.
    > > > > --
    > > > > David

    > >
    > > > Use ASHX generic handler

    > >
    > > > <img src="image.ashx">

    > >
    > > > Let me know if I should help with the code for ashx
    > > > .- Hide quoted text -

    > >
    > > - 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
    > .
    >
    DavidC, Feb 4, 2010
    #5
  6. On Feb 4, 6:49 pm, DavidC <> wrote:
    > Thanks. Now I just have to convert your C# to VB.
    > --
    > David
    >
    >
    >
    > "Alexey Smirnov" wrote:
    > > On Feb 4, 2:46 pm, DavidC <> wrote:
    > > > Yes, please. I am not familiar with ashx. Thanks.
    > > > --
    > > > David

    >
    > > > "Alexey Smirnov" wrote:
    > > > > On Feb 3, 11:26 pm, DavidC <> wrote:
    > > > > > 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.
    > > > > > --
    > > > > > David

    >
    > > > > Use ASHX generic handler

    >
    > > > > <img src="image.ashx">

    >
    > > > > Let me know if I should help with the code for ashx
    > > > > .- Hide quoted text -

    >
    > > > - 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 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!
    Alexey Smirnov, Feb 4, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lamine Darbouche

    How to populate a checkbox from a database

    Lamine Darbouche, Feb 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,589
    Itai Raz
    Feb 16, 2004
  2. Mark
    Replies:
    9
    Views:
    5,091
  3. Yuriy

    How to populate PlaceHolder

    Yuriy, Aug 23, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    5,073
    David Wier
    Aug 26, 2003
  4. Laszlo Zsolt Nagy
    Replies:
    1
    Views:
    1,236
    Kartic
    Jan 26, 2005
  5. Enjoy Life
    Replies:
    2
    Views:
    252
    Roland Hall
    Feb 23, 2005
Loading...

Share This Page