我正在ListView中从Vk.com加载一堆图像。我希望能够将任何列表中的所有图像下载/保存到手机的图库中,或者在一个简单的事件中或者默认情况下。我该怎么做? 这是XAML。
<Page.BottomAppBar>
<CommandBar>
<AppBarButton FontSize="15" FontFamily="Times New Roman" Icon="Back" Label="Albums" Click="AppBarButton_Click"/>
</CommandBar>
</Page.BottomAppBar>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"> </RowDefinition>
</Grid.RowDefinitions>
<TextBlock x:Name="MyTextBlock" FontSize="35" HorizontalAlignment="Center"></TextBlock>
<ListView Grid.Row="1"
ItemsSource="{Binding}"
SelectionMode="None"
IsItemClickEnabled="False"
>
<ListView.ItemTemplate>
<DataTemplate>
<Image Stretch="UniformToFill" Source="{Binding image_final}"
AutomationProperties.Name="{Binding Title}"
Width="Auto"
Height="Auto"
></Image>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
答案 0 :(得分:2)
您已将其标记为WP8和WP8.1。下面的代码是8,但不应该为8.1做太多工作。
您可以将图像下载并保存到用户库,如下所示:
using Microsoft.Xna.Framework.Media;
using System.Windows.Media.Imaging;
using System.IO;
WebClient client = new WebClient();
client.OpenReadCompleted += (s, ev) =>
{
var streamResourceInfo = new StreamResourceInfo(ev.Result, null);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(streamResourceInfo.Stream);
WriteableBitmap bmp = new WriteableBitmap(bitmap);
MediaLibrary library = new MediaLibrary();
using (MemoryStream stream = new MemoryStream())
{
Extensions.SaveJpeg(bmp, stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
library.SavePicture("imagename.jpg", stream.ToArray());
}
MessageBox.Show("The picture has been saved to your Pictures Hub", "Success!", MessageBoxButton.OK);
};
client.OpenReadAsync(new Uri("http://example.com/imageurl.jpg"));
显然,在最后一行放置了正确的图片网址。并可选择将"imagename.jpg"
替换为图像的真实名称。
答案 1 :(得分:0)
此链接确实解释了所有内容:保存图像并将其组织到文件夹中。绝对值得一读。
说明:
//Create a storage folder in the Pictures Library:
StorageFolder AppFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("XYZ", CreationCollisionOption.OpenIfExists);
//Naming and storing the file:
var uri = imageuri; //image uri
var filename = Path.GetFileName(uri.LocalPath); //designates file name
var thumbnail = RandomAccessStreamReference.CreateFromUri(uri);
var remoteFile = await StorageFile.CreateStreamedFileFromUriAsync(filename, uri, thumbnail);
await remoteFile.CopyAsync(albumname, filename, NameCollisionOption.FailIfExists); //Fail if argument filename is a string by which a file is already saved.
我希望这会有所帮助。