0

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

    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;
    }
4

1 回答 1

0

您需要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. 这是启动过程的句柄,可用于确定何时完成。

于 2012-10-23T08:21:12.417 回答