locked
Bind Image with Byte array ( byte[] ) RRS feed

  • Question

  • User-7971440 posted

    Hi,

    I am trying to bind image to a grid view.

    I have Byte array data of a image with me and i need to bind that data to my asp.net image control.

    I've tried all the options found in the google but image is not reflecting

    here is my code 

    insite RowDataBound

    System.Web.UI.WebControls.Image imgCmpImage = (System.Web.UI.WebControls.Image)e.Row.FindControl("ImgCampaingImg");
    System.Web.UI.WebControls.Label labelText = (System.Web.UI.WebControls.Label)e.Row.FindControl("imgData");
    imgCmpImage.ImageUrl = "~/Handlers/Campaigns.ashx?Path=" + labelText.Text + "&" + "source=" + "VAIC";

    handler code

    string strImgPath = context.Request.QueryString["Path"].ToString();
    byte[] byteArray = ASCIIEncoding.ASCII.GetBytes(strImgPath);

    context.Response.ContentType = "image/jpg";

    context.Response.OutputStream.Write(byteArray, 0, byteArray.Length);

    Output : image src

    src="~/Handlers/Campaigns.ashx?Path=+DQoJPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4NCgkJPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNS4xIFdpbmRvd3MiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6NzE5RERGNTk5QzNBMTFFMjhFOEU5NUEzMTA2NjlDNkUiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6NzE5RERGNUE5QzNBMTFFMjhFOEU5NUEzMTA2NjlDNkUiPg0KCQkJPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6NzE5RERGNTc5QzNBMTFFMjhFOEU5NUEzMTA2NjlDNkUiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6NzE5RERGNTg5QzNBMTFFMjhFOEU5NUEzMTA2NjlDNkUiLz4NCgkJPC9yZGY6RGVzY3JpcHRpb24+DQoJCTxyZGY6RGVzY3JpcHRpb24geG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIj48ZGM6dGl0bGU+PHJkZjpBbHQgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj48cmRmOmxpIHhtbDpsYW5nPSJ4LWRlZmF1bHQiPmNvbWVfZ3Jvd19hcnRfMTMwNDAzPC9yZGY6bGk+PC9yZGY6QWx0Pg0KCQkJPC9kYzp0aXRsZT48L3JkZjpEZXNjcmlwdGlvbj48cmRmOkRlc2NyaXB0aW9uIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyI+PGRjOmRlc2NyaXB0aW9uPjxyZGY6QWx0IHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+PHJkZjpsaSB4bWw6bGFuZz0ieC1kZWZhdWx0Ij5jb21lX2dyb3dfYXJ0XzEzMDQwMzwvcmRmOmxpPjwvcmRmOkFsdD4NCgkJCTwvZGM6ZGVzY3JpcHRpb24+PC9yZGY6RGVzY3JpcHRpb24+PC9yZGY6UkRGPg0KPC94OnhtcG1ldGE+DQo8P3hwYWNrZXQgZW5kPSd3Jz8+/9sAQwACAQECAQECAgICAgIC&source=VAIC

    Thanks in advance.


    Friday, May 17, 2013 7:30 AM

Answers

  • User-1185172246 posted

    What is the Path param? Is it the binary content of your image? If yes, then should change this line:

    byte[] byteArray = ASCIIEncoding.ASCII.GetBytes(strImgPath);

    by this one:

    byte[] byteArray = Convert.FromBase64String(strImgPath);
    




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 17, 2013 10:44 PM

All replies

  • User1992938117 posted

    I've tried all the options found in the google but image is not reflecting

    Try as described in below article

    http://www.aspsnippets.com/articles/display-images-from-sql-server-database-in-asp.net-gridview-control.aspx

    Also can download sample code.

    Friday, May 17, 2013 7:36 AM
  • User-7971440 posted

    Hi, Thanks for reply

    I'm Not using Sql so i dont have any command and other sql objects in my code. but i have byte[] so, i kept like this 

    string strImgPath = context.Request.QueryString["Path"].ToString();
    byte[] byteArray = ASCIIEncoding.ASCII.GetBytes(strImgPath);

    context.Response.ContentType = "image/jpg";
    context.Response.BinaryWrite(byteArray);

    But it didnt work. 

    Friday, May 17, 2013 8:52 AM
  • User-1185172246 posted

    What is the Path param? Is it the binary content of your image? If yes, then should change this line:

    byte[] byteArray = ASCIIEncoding.ASCII.GetBytes(strImgPath);

    by this one:

    byte[] byteArray = Convert.FromBase64String(strImgPath);
    




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 17, 2013 10:44 PM