C# WCF/REST是否将图像放入picturebox?
因此,我有一个wcf rest服务,它成功地从控制台应用程序运行,如果我导航到:我的图像显示注意,300/400设置html页面主体内图像的宽度和高度 代码如下所示:C# WCF/REST是否将图像放入picturebox?,c#,wpf,web-services,rest,C#,Wpf,Web Services,Rest,因此,我有一个wcf rest服务,它成功地从控制台应用程序运行,如果我导航到:我的图像显示注意,300/400设置html页面主体内图像的宽度和高度 代码如下所示: namespace WcfServiceLibrary1 { [ServiceContract] public interface IReceiveData { [OperationContract] [WebInvoke(Method = "GET", BodyStyle
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;
}
}
}
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);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox.Image = GetImage(400, 500); // or any other size here
}
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));
}
使用基于WinForms的方法获取具有可变宽度和高度的图像如下所示:
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;
}
}
}
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);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox.Image = GetImage(400, 500); // or any other size here
}
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));
}
您可以将图像放入如下图片框:
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;
}
}
}
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);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox.Image = GetImage(400, 500); // or any other size here
}
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));
}
在WPF中,您可以执行以下操作:
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;
}
}
}
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);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox.Image = GetImage(400, 500); // or any other size here
}
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));
}
请写出您对在请求URL中用“/”分隔宽度和高度参数的评论。更为RESTful的方法是将其作为参数传递,例如URL如下所示:
http://localhost:8000/Service/picture?width=480&height=640
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture?width={width}&height={height}")]
您的WebInvoke看起来像:
http://localhost:8000/Service/picture?width=480&height=640
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture?width={width}&height={height}")]
GetImage方法无需更改,参数的填充方式将与原始UriTemplate相同。System.IO.Stream不包含CopyTo的定义?请参阅编辑。更简单。CopyTo在.NET4.0中,但您不需要它。是的,它的工作原理令人惊讶,谢谢您的wpf!就像你能读懂我的心思一样!David请看,我能问一下为什么图片?宽度=480和高度=640更安静?因为宽度和高度是图片资源的属性,而不是资源本身的属性。