Shr*_*rma 2 .net c# download microsoft-metro windows-8
我正在尝试开发一个简单的Windows 8 Metro应用程序,它只是从给定的URL下载图像文件(比如http://sample.com/foo.jpg),然后将其保存到图片库.
我在UI中有一个图像控件来显示下载的图像.我也很难将图像控件的图像源设置为新下载的图像(实际上我甚至无法下载它).
此外,是否可以将图像文件存储在图片库中的特定文件夹中(如果它不存在,那么应用程序应该创建它)?
我真的被困在这里了.
请帮我.
这是一些我认为可以实现你想要的粗略代码.它假设您有两个图像控件(Image1和Image2),并且您在清单中检查了图片库功能.看看在XAML图像样本,以及
Uri uri = new Uri("http://www.picsimages.net/photo/lebron-james/lebron-james_1312647633.jpg");
var fileName = Guid.NewGuid().ToString() + ".jpg";
// download pic
var bitmapImage = new BitmapImage();
var httpClient = new HttpClient();
var httpResponse = await httpClient.GetAsync(uri);
byte[] b = await httpResponse.Content.ReadAsByteArrayAsync();
// create a new in memory stream and datawriter
using (var stream = new InMemoryRandomAccessStream())
{
using (DataWriter dw = new DataWriter(stream))
{
// write the raw bytes and store
dw.WriteBytes(b);
await dw.StoreAsync();
// set the image source
stream.Seek(0);
bitmapImage.SetSource(stream);
// set image in first control
Image1.Source = bitmapImage;
// write to pictures library
var storageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
fileName,
CreationCollisionOption.ReplaceExisting);
using (var storageStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
await RandomAccessStream.CopyAndCloseAsync(stream.GetInputStreamAt(0), storageStream.GetOutputStreamAt(0));
}
}
}
// read from pictures library
var pictureFile = await KnownFolders.PicturesLibrary.GetFileAsync(fileName);
using ( var pictureStream = await pictureFile.OpenAsync(FileAccessMode.Read) )
{
bitmapImage.SetSource(pictureStream);
}
Image2.Source = bitmapImage;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3906 次 |
| 最近记录: |