【问题标题】:How to convert and store bitmapImages in Windows8如何在 Windows 8 中转换和存储位图图像
【发布时间】:2012-10-23 06:55:09
【问题描述】:

我想将位图图像存储在本地目录中。我写了那些代码。但是发生了未知错误,无法编译。请告诉我错误的原因以及转换和存储位图图像的正确方法。

    void StoreAndGetBitmapImage()
    {
        BitmapImage image = new BitmapImage(new Uri("ms-appx:///Assets/" + "test.png"));
        StorageFile storageFile = ConvertBitmapImageIntoStorageFile(image, "image_name");
        StoreStorageFile(storageFile);
        BitmapImage resultImage = GetBitmapImage("image_name");
    }

    StorageFile ConvertBitmapImageIntoStorageFile(BitmapImage bitmapImage,string fileName)
    {
        StorageFile file = Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource).GetResults();
        file.RenameAsync(fileName);
        return file;
    }

    void StoreStorageFile(StorageFile storageFile)
    {
        storageFile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
    }

    BitmapImage GetBitmapImage(string fileName)
    {
        BitmapImage bitmapImage;
        bitmapImage = new BitmapImage();

        bitmapImage.UriSource = new Uri(new Uri(
             Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\" +
             Windows.Storage.ApplicationData.Current.LocalFolder.Name),
             fileName);

        return bitmapImage;
    }

【问题讨论】:

    标签: windows-8 windows-runtime windows-store-apps


    【解决方案1】:

    你需要await异步方法调用。因此,您必须将该方法声明为异步。例如:

    async Task<StorageFile> ConvertBitmapImageIntoStorageFile(BitmapImage bitmapImage,string fileName)
    {
        StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource);
        await file.RenameAsync(fileName);
        return file;
    }
    

    await 导致从方法返回。如果任务完成,该方法将在这个位置继续(可能在不同的线程中)。异步方法返回一个 IAsyncOperation 对象,例如Task。这是启动过程的句柄,可用于确定何时完成。

    【讨论】:

    • 谢谢尼科!我没有故意使用“等待”,因为我想同步运行这个方法。
    • 那么你必须在异步操作的返回值上调用Wait()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多