Windows 8 如何在Windows8中转换和存储位图图像
我想在本地目录中存储位图图像。我编写了这些代码。但是发生了未知错误,无法编译。请告诉我错误的原因以及转换和存储位图图像的正确方法Windows 8 如何在Windows8中转换和存储位图图像,windows-8,windows-runtime,windows-store-apps,Windows 8,Windows Runtime,Windows Store Apps,我想在本地目录中存储位图图像。我编写了这些代码。但是发生了未知错误,无法编译。请告诉我错误的原因以及转换和存储位图图像的正确方法 void StoreAndGetBitmapImage() { BitmapImage image = new BitmapImage(new Uri("ms-appx:///Assets/" + "test.png")); StorageFile storageFile = ConvertBitmapImageInto
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;
}
您需要等待异步方法调用。因此,必须将该方法声明为async。例如:
async Task<StorageFile> ConvertBitmapImageIntoStorageFile(BitmapImage bitmapImage,string fileName)
{
StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource);
await file.RenameAsync(fileName);
return file;
}
异步任务转换器BitMapImageToStorageFile(BitMapImageBitMapImageString文件名)
{
StorageFile file=等待Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource);
wait file.RenameAsync(文件名);
返回文件;
}
wait
导致从方法返回。如果任务完成,方法将在这个位置继续(可能在不同的线程中)。异步方法返回一个IAsyncOperation
对象,例如任务
。这是启动过程的句柄,可用于确定何时完成。谢谢Nico!我不是故意使用“Wait”,因为我想同步运行这个方法。然后你必须对异步操作的返回值调用Wait()
。