WCF / REST Get picture-in-picture?

So I have a wcf rest service that starts successfully from a console app, if I go to: http: // localhost: 8000 / Service / picture / 300/400 my image is displayed, notice the 300/400 sets the width and height images in the body of the html page.

The code looks like this:

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;
        }
    }
}

      

What I want to do now is use my Windows Forms Application client and add this image to the picture. Im abit got stuck on how this can be achieved, since I would like the width and height of the image from my wcf service to be set to the width and height of the image. I tried this, but there are errors on two of the lines and I'm not even sure if it will work as the code for my wcf serving service separates the width and height with a "/" if you notice in the 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();
    }

      

So just wondering if anyone can help me with this futile attempt to add the resizable image size from my rest service to the image window on button click.

This is the host application:

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


source to share


2 answers


A WinForms based method to get an image with variable width and height would look like this:

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

      

You can put an image in a PictureBox like this:



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

      

In WPF, you would do something like this:

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

      

+3


source


WRT your comment about width and height parameters separated by "/ in your request url", a more RESTful approach was to pass them as parameters, eg. The URL will look like this:

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

      

and your WebInvoke will look like this:



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

      

The GetImage method should not change, the parameters will be filled in the same way as the original UriTemplate.

+1


source







All Articles