2015-02-07 61 views
0

我想在(Asp.Net的圖像控件)客戶端顯示圖像(.jpeg)。這段代碼有什麼問題?使用WCF Rest服務顯示圖像

我的休息服務代碼如下,其中我從數據庫中獲取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。遇到意外字符'ÿ'。

+0

至於我對WCF休息服務的新手。 – Sheeba 2015-02-07 05:44:06

+1

這是一個類似的問題,請通過答案:http://stackoverflow.com/questions/13445227/restful-service-returns-there-was-an-error-checking-start-element-of-object-of – 2015-02-07 05:58:16

回答

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; 

}