简体   繁体   English

如何使用Web服务检索图像到ASPX网站C#?

[英]How to retrieve an image using web service to aspx website C#?

I would like to know how can I retrieve an image using web service to aspx website C#? 我想知道如何使用Web服务将图像检索到ASPX网站C#? My web service code is below. 我的Web服务代码如下。

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;
}

May I ask what's the benefit of using web service while you are using ASP.NET page? 请问在使用ASP.NET页面时使用Web服务有什么好处? 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: Web服务(尤其是SOAP)期望带有调用详细信息的XML信封之类的东西。最好使用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 来源: ASP.NET从.aspx链接返回图像

btw, to return an image in a web service here 's the sample code. 顺便说一句,要返回Web服务中的图像, 这里是示例代码。

Update: also found this which has a sample source code to retrieve image throw web service. 更新:还发现其中有一个示例源代码来获取图像抛Web服务。

Note that you cannot bind the return of the web service to the image in the HTML code directly. 请注意,您不能将Web服务的返回直接绑定到HTML代码中的图像。 may be a little JQuery techniques needed for this. 可能需要一些JQuery技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM