我已截取屏幕截图并将其作为jpg图像保存在应用程序的本地文件夹中。现在我想在列表中检索这些图像并与类中的列表视图绑定。请帮助我如何做到这一点。 我已通过此代码保存图像
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Window.Current.Content);
//create unique file in LocalFolder
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("screenshotCapture.jpg", CreationCollisionOption.GenerateUniqueName);
//create JPEG image
using (var stream = await file.OpenStreamForWriteAsync())
{
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream.AsRandomAccessStream());
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight, logicalDpi, logicalDpi,
pixelBuffer.ToArray());
FileSavePicker filesave = new FileSavePicker();
StorageFolder storagefolder = ApplicationData.Current.LocalFolder;
var screenshotsfolder = await storagefolder.CreateFolderAsync("screenshotCapture", CreationCollisionOption.OpenIfExists);
var Screenshot_image = await file.CopyAsync(screenshotsfolder, "screenshot" + DateTime.Now.ToFileTime() + ".jpg");
await encoder.FlushAsync();
现在我想从列表中的本地文件夹屏幕截图中获取所有图像。
答案 0 :(得分:1)
根据您的代码,您似乎已将截图存储在" screenshotCapture"夹。所以我们可以像下面这样检索它们:
var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("screenshotCapture");
var files = await folder.GetFilesAsync();
我们拥有这些StorageFile
后,我们可以使用它们来创建BitmapImage
作为ListView
的来源,如:
ObservableCollection<BitmapImage> ImgList = new ObservableCollection<BitmapImage>();
foreach (var file in files)
{
using (var stream = await file.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
ImgList.Add(bitmapImage);
}
}
以下是完整的样本:
在XAML中
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{x:Bind ImgList}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="BitmapImage">
<Image Width="500"
Height="200"
Source="{x:Bind }"
Stretch="Fill" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
在代码隐藏
中public sealed partial class MainPage : Page
{
private ObservableCollection<BitmapImage> ImgList = new ObservableCollection<BitmapImage>();
public MainPage()
{
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("screenshotCapture");
var files = await folder.GetFilesAsync();
foreach (var file in files)
{
using (var stream = await file.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
ImgList.Add(bitmapImage);
}
}
}
}