1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

gettin problem in fetch image in asp.net page;

Discussion in 'C#' started by amkeabhi123, Jul 25, 2012.

  1. #1
    Hello,
    i am working asp.net fetch image program in the database, image should be store in BLOB form so i want to display image in asp.net page in single side.
    when i called
    Response.BinaryWrite() method it is display full page in asp.net, i want display only single side in the page.

    thanks in advance.
     
    amkeabhi123, Jul 25, 2012 IP
  2. DaveInFL

    DaveInFL Peon

    Messages:
    125
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Best bet would be adding an httphandler (they have the extension ashx) and can be added from the Add New Item menu in visual studio.
    A handler for your app would look something like this.

    Handler.ashx file to perform image retrieval. This Handler.ashx page will contain only one method called ProcessRequest. This method will return binary data to the incoming request. In this method, we do normal data retrieval process and return only the Image_Content field as bytes of array.

    The sample code follows public void ProcessRequest (HttpContext context)
    {
    SqlConnection myConnection = new SqlConnection(“YourConnectionString”);
    myConnection.Open();
    string sql = "Select Image_Content, Image_Type from ImageGallery where Img_Id=@ImageId";
    SqlCommand cmd = new SqlCommand(sql, myConnection);
    cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = context.Request.QueryString["id"];
    cmd.Prepare();
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();
    context.Response.ContentType = dr["Image_Type"].ToString();
    context.Response.BinaryWrite((byte[])dr["Image_Content"]);
    dr.Close();
    myConnection.Close();

    }

    To use this method your image source will be the handler.


    Example:
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?id=" + Eval("Img_Id") %>' />
    </ItemTemplate>
    </asp:TemplateField>

    A complete sample can be found aspdotnetcodes_com/Insert_Images_Database.aspx I would also recommend the original Asp.net (found as asp dot net) classified starter kit to learn about binary image storage and retrieval for display, it is very thorough.
     
    DaveInFL, Jul 31, 2012 IP
  3. veganin

    veganin Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    I agree that handler would be the way to go if you want to/ have to store images in DB. However I would highly recommend storing images in the file system instead.
     
    veganin, Aug 14, 2012 IP