从应用程序中检索图像';的本地文件夹

本文关键字:文件夹 应用程序 检索 图像 | 更新日期: 2023-09-27 18:01:14

我已经拍下了屏幕截图,并将它们以jpg图像的形式保存在应用程序的本地文件夹中。现在我想在列表中检索这些图像,并与类中的listview绑定。请帮我怎么做。我已经通过这个代码保存了图像

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

现在我想在一个列表中完成本地文件夹屏幕截图中的所有图像。

从应用程序中检索图像';的本地文件夹

根据您的代码,您似乎已将屏幕截图存储在"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);
            }
        }
    }
}