因此,如果我導航到: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();
System.IO.Stream不包含CopyTo的定義? – 2012-04-04 17:31:09
請參閱編輯。它更簡單。 CopyTo在.Net 4.0中,但你不需要它。 – Clemens 2012-04-04 17:33:37
是的,它真的令人驚訝,它的工作原理和謝謝你的WPF!幾乎像你讀我的腦海! – 2012-04-04 17:34:59