Windows 8 通用应用程序的 ASP.Net web Api

标签: Asp.net windows
发布时间: 2016/7/12 19:06:07
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想从 ASP.NET Web Api 服务绑定图像我有控制器

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

客户端是 Windows 8 的通用应用程序有下一个代码

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

我不知道该怎么办,我没法在 BitmapImage 文件中,例如让影像。

解决方法 1:

第二方式如果你想要得到很多的照片是,你可以在 API 文件夹中创建新文件夹并命名为例如 PhotoRepository 在此文件夹中添加照片和得到照片通过它的 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;
            }
赞助商