所以我有一个wcf rest服务,它可以从一个控制台应用程序运行,如果我导航到:http://localhost:8000/Service/picture/300/400我的图像显示注意300/400设置图像的宽度和高度在html的正文中页。
代码如下所示:
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窗体应用程序”并将该图像添加到图片框中。因为我希望wcf休息服务中图像的宽度和高度由图片框的宽度和高度设置,所以我知道如何实现这一点。我试过这个但是在两行上有错误,我甚至不确定它是否会起作用我的wcf休息服务的代码用“/”分隔宽度和高度,如果你在网址中注意到的话。
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();
}
所以只是想知道是否有人可以帮我解决这个徒劳的尝试,从我的休息服务到按钮点击的图片框添加可变图像大小。
这是主机应用程序:
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();
答案 0 :(得分: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));
}
答案 1 :(得分:1)
在你的请求URL中写下关于宽度和高度参数被'/'分隔的注释,一个更RESTful的方法是将它们作为参数传递给你,例如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相同。