UWP C#来自streamreader的图片(请求帖子)

时间:2017-08-29 20:24:20

标签: c# uwp uwp-xaml

我尝试在我的UWP应用程序中放置一个图表,所以我找到的最简单的方法是在我的wfp中使用Image controle并从googleapis_chart获取图像图表 但如何将StreamReader转换为Image?

        string postString = "cht=lc&chs=180x180&chxt=x&chd=t:"+data;
        WebRequest req_googlechart = WebRequest.Create("https://chart.googleapis.com/chart");
        req_googlechart.Method = "POST";
        req_googlechart.ContentType = "application/x-www-form-urlencoded";
        Stream stream_googlechart = await req_googlechart.GetRequestStreamAsync();
        StreamWriter requestWriter = new StreamWriter(stream_googlechart);
        await requestWriter.WriteAsync(postString);
        requestWriter.Dispose();
        WebResponse rep_googlechart = await req_googlechart.GetResponseAsync();
        StreamReader reader_googlechart = new StreamReader(rep_googlechart.GetResponseStream());

1 个答案:

答案 0 :(得分:0)

reader_googlechart .BaseStream的类型为Stream,您可以使用以下代码将其转换为BitmapImage。并使用BitmapImage设置图像源。

Stream res = reader_googlechart.BaseStream;
var bitmap = new BitmapImage();
using (var mem = new MemoryStream())
{
    await res.CopyToAsync(mem);
    mem.Position = 0;
    await bitmap.SetSourceAsync(mem.AsRandomAccessStream());
}
this.img.Source = bitmap;