2012-04-04 62 views
3

因此,如果我導航到:http://localhost:8000/Service/picture/300/400我的圖像顯示注意300/400設置圖像的寬度和高度在html頁面的主體內,所以我有wcf休息服務,從控制檯應用成功運行。WCF/REST獲取圖像到圖片框?

的代碼看起來是這樣的:

namespace WcfServiceLibrary1 
{ 
    [ServiceContract] 
    public interface IReceiveData 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture/{width}/{height}")] 
     Stream GetImage(string width, string height); 
    } 
    public class RawDataService : IReceiveData 
    { 
     public Stream GetImage(string width, string height) 
     { 
       int w, h; 

       if (!Int32.TryParse(width, out w)) 
       { 
        w = 640; 
       } 
       // Handle error 
       if (!Int32.TryParse(height, out h)) 
       { 
        h = 400; 
       } 
      Bitmap bitmap = new Bitmap(w, h); 
      for (int i = 0; i < bitmap.Width; i++) 
      { 
       for (int j = 0; j < bitmap.Height; j++) 
       { 
        bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue : Color.Yellow); 
       } 
      } 
      MemoryStream ms = new MemoryStream(); 
      bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
      ms.Position = 0; 
      WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; 
      return ms; 
     } 
    } 
} 

我想現在要做的是使用客戶端應用程序「我的Windows應用程序形成」,並添加圖像到一個PictureBox。我不知道如何實現這一點,因爲我希望我的wcf rest服務中的圖像的寬度和高度由picturebox的寬度和高度來設置。我已經嘗試了這一點,但在兩行中有錯誤,即時通訊甚至不知道如果它會工作,因爲我的wcf休息服務的代碼分離寬度和高度與「/」,如果你注意到的URL。

string uri = "http://localhost:8080/Service/picture"; 
    private void button1_Click(object sender, EventArgs e) 
    { 

     StringBuilder sb = new StringBuilder(); 
     sb.AppendLine("<picture>"); 
     sb.AppendLine("<width>" + pictureBox1.Image.Width + "</width>"); 
     // the url looks like this http://localhost:8080/Service/picture/300/400 when accessing the image so I am trying to set this here 
     sb.AppendLine("<height>" + pictureBox1.Image.Height + "</height>"); 
     sb.AppendLine("</picture>"); 
     string picture = sb.ToString(); 
     byte[] getimage = Encoding.UTF8.GetBytes(picture); // not sure this is right 
     HttpWebRequest req = WebRequest.Create(uri); //cant convert webrequest to httpwebrequest 
     req.Method = "GET"; 
     req.ContentType = "image/jpg"; 
     req.ContentLength = getimage.Length; 
     MemoryStream reqStrm = req.GetRequestStream(); //cant convert IO stream to IO Memory stream 
     reqStrm.Write(getimage, 0, getimage.Length); 
     reqStrm.Close(); 
     HttpWebResponse resp = req.GetResponse(); // cant convert web respone to httpwebresponse 
     MessageBox.Show(resp.StatusDescription); 
     pictureBox1.Image = Image.FromStream(reqStrm); 
     reqStrm.Close(); 
     resp.Close(); 
    } 

所以只是想知道,如果有人可以幫助我這個妄圖從我的REST服務上點擊按鈕添加一個可變圖像尺寸的圖片框。

這是主機應用藏漢:

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
      ServiceHost host = new ServiceHost(typeof(RawDataService), new Uri(baseAddress)); 
      host.AddServiceEndpoint(typeof(IReceiveData), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); 
      host.Open(); 
      Console.WriteLine("Host opened"); 
      Console.ReadLine(); 

回答

3

一個WinForms爲基礎的方法來獲取具有可變寬度和高度的圖像是這樣的:

public Image GetImage(int width, int height) 
{ 
    string uri = string.Format("http://localhost:8000/Service/picture/{0}/{1}", width, height); 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
    { 
     using (Stream stream = response.GetResponseStream()) 
     { 
      return new Bitmap(stream); 
     } 
    } 
} 

你可以把圖像一個PictureBox是這樣的:

private void Form1_Load(object sender, EventArgs e) 
{ 
    pictureBox.Image = GetImage(400, 500); // or any other size here 
} 

在WPF中你會這樣做:

public ImageSource GetImage(int width, int height) 
{ 
    string uri = string.Format("http://localhost:8000/Service/picture/{0}/{1}", width, height); 
    return BitmapFrame.Create(new Uri(uri)); 
} 
+0

System.IO.Stream不包含CopyTo的定義? – 2012-04-04 17:31:09

+0

請參閱編輯。它更簡單。 CopyTo在.Net 4.0中,但你不需要它。 – Clemens 2012-04-04 17:33:37

+0

是的,它真的令人驚訝,它的工作原理和謝謝你的WPF!幾乎像你讀我的腦海! – 2012-04-04 17:34:59

1

WRT您對有關在請求URL中用'/'分隔的寬度和高度參數的評論,更多REST風格的方法是將它們作爲參數傳遞,例如, URL可能看起來像:

http://localhost:8000/Service/picture?width=480&height=640 

和你WebInvoke會是什麼樣子:

[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture?width={width}&height={height}")] 

該方法的getImage不需要改變,因爲他們與原UriTemplate的參數將被填充相同。

+0

David請參閱http://stackoverflow.com/questions/10012737/body-parameter-width-get-operations-cannot-have-a-body – 2012-04-04 18:11:54

+0

我可以問問爲什麼圖片?width = 480&height = 640更安靜? – 2012-04-04 18:12:27

+0

因爲寬度和高度是圖片資源的屬性,而不是資源本身。 – Opsimath 2012-04-04 21:28:40