这段代码出了什么问题?使用WCF Rest服务显示图像

时间:2015-02-07 05:42:51

标签: c# asp.net wcf rest

我想在(Asp.Net的图像控件)客户端上显示Image(.jpeg)。

我的休息服务代码如下,我从数据库中获取ImagePath。

public Stream getImage(string width, string height)
{          
    int iRecordid = 1;           
    var q = from c in db.TblNames
            where c.RecordId == iRecordid
            select new { c.ImgName, c.ImgPath };

    foreach (var obj1 in q)
    {
        sImageName = obj1.ImgName;
        sImagePath = obj1.ImgPath;
    }           
    MemoryStream ms = new MemoryStream();

    ms.Position = 0;
    return ms;
}

我在客户端使用此服务赞

 public void getImage()
 {  
     string url = @"http://localhost:50353/CustomerService.svc/getImage/100/200";

     var service = new ServiceReference(url);

     WebClient wc = new WebClient();

     byte[] res1 = wc.DownloadData(url);

     Stream res2 = new MemoryStream(res1);

     DataContractJsonSerializer res3 = new DataContractJsonSerializer(typeof(ImageMap));
     string ss = res3.ReadObject(res2).ToString();
     Image1.ImageUrl = ss;
 }

我收到错误

  

反序列化System.Web.UI.WebControls.ImageMap类型的对象时出错。遇到意外的角色'ÿ'。

1 个答案:

答案 0 :(得分:1)

请尝试下面的休息服务端代码

    public string getImage(string width, string height)
        {

        int iRecordid = 1;

        var q = from c in db.MasterStructures 
                where c.RecordId == iRecordid
                select new { c.ImgName, c.ImgPath };

        foreach (var obj1 in q)
        {
            sImageName = obj1.ImgName;
            sImagePath = obj1.ImgPath;
        }           
        MemoryStream ms = new MemoryStream();

        ms.Position = 0;
        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
        return sImagePath;

}