1

I would like to know how can I retrieve an image using web service to aspx website C#? My web service code is below.

public List<byte[]> getBlueBallImageDefault()
{
    string strCMD = "SELECT blueBallImage FROM CorrespondingBall WHERE objective = 0";
    SqlCommand cmd = new SqlCommand(strCMD, conn);
    SqlDataReader dr = cmd.ExecuteReader();

    List<byte[]> blueBallImageDefaultByteList = new List<byte[]>();
    while (dr.Read())
    {
        blueBallImageDefaultByteList.Add((byte[])dr["blueBallImage"]);
    }
    return blueBallImageDefaultByteList;
}

[WebMethod]
public List<byte[]> getBlueBallImageDefault()
{
    List<byte[]> blueBallImageDefaultByteList = new List<byte[]>();
    con.dbConnect();
    blueBallImageDefaultByteList = con.getBlueBallImageDefault();
    con.dbClose();

    return blueBallImageDefaultByteList;
}
Liu Jia Hui
  • 231
  • 2
  • 8
  • 16

1 Answers1

0

May I ask what's the benefit of using web service while you are using ASP.NET page? Web services, especially SOAP expect things like an XML envelope with the details of the call in. You'd be better off using a HttpHandler:

public class ImageHandler : IHttpHandler 
{ 
  public bool IsReusable { get { return true; } } 

  public void ProcessRequest(HttpContext ctx) 
  { 
    var myImage = GetImageSomeHow();
    ctx.Response.ContentType = "image/png"; 
    ctx.Response.OutputStream.Write(myImage); 
  } 
}

source: ASP.NET Return image from .aspx link

btw, to return an image in a web service here's the sample code.

Update: also found this which has a sample source code to retrieve image throw web service.

Note that you cannot bind the return of the web service to the image in the HTML code directly. may be a little JQuery techniques needed for this.

Community
  • 1
  • 1
Afshin
  • 1,222
  • 11
  • 29