如何在Windows 8中转换和存储位图图像

时间:2012-10-23 06:55:09

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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