Search code examples
asp.netwindowsstore

Windows 8 universal app ASP.Net web Api


I'm trying binding image from ASP.NET Web Api service there i have contorller

public class ImageController : ApiController
{

    public HttpResponseMessage GetImage()
    {
        HttpResponseMessage response = new HttpResponseMessage();
        response.Content = new StreamContent(new FileStream("FileAddress", FileMode.Open));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
        return response;
    } 
}

client side is Windows 8 universal app there are next code

private async void Button_Click(object sender, RoutedEventArgs e)
{
    Uri datauri = new Uri("http://localhost:63606/Api/Image");
    var client = new HttpClient();
    var datafil = await client.GetAsync(datauri);
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, datauri);

    HttpResponseMessage response = await client.SendRequestAsync(request, HttpCompletionOption.ResponseHeadersRead);
    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
    DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
}

I don't know what to do , I couldn't get image for example in BitmapImage file.


Solution

  • Second way if you want get many photo is that you can create new folder in API folder and named it for example PhotoRepository add photo in this folder and get photo via it URI

        private void Button_Click(object sender, RoutedEventArgs e)
                {
                    Uri datauri = new Uri("http://localhost:63606/PhotoReposytory/"photo name".jpg"); 
    //jpg  or other format
                    BitmapImage foto = new BitmapImage(datauri);
                    Image1.Source = foto;
                }