gpt4 book ai didi

c# - WCF/REST 将图像放入图片框?

转载 作者:行者123 更新时间:2023-11-30 15:39:49 25 4
gpt4 key购买 nike

因此,如果我导航到:http://localhost:8000/Service/picture/300/400,我就有了从控制台应用程序成功运行的 wcf 休息服务。我的图像显示注意 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 rest 服务中的图像的宽度和高度由图片框的宽度和高度设置。我已经试过了,但是有两行有错误,我什至不确定它是否会工作,因为如果你在 url 中注意到,我的 wcf rest 服务的代码用“/”分隔宽度和高度。

    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();

最佳答案

获取具有可变宽度和高度的图像的基于 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));
}

关于c# - WCF/REST 将图像放入图片框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10014793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com