0

我使用下面的代碼捕獲照片,但不知道如何將其保存在圖片庫或圖片庫中的文件夾中。希望你對此有所幫助。如何將照相機捕獲的照片保存在圖片庫中

CameraCaptureUI camera = new CameraCaptureUI(); 
camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9); 
StorageFile photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo); 


if (photo != null) 
{ 
    BitmapImage bmp = new BitmapImage(); 
    IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read); 
    bmp.SetSource(stream); 
    ImageSource.Source = bmp; 
    ImageSource.Visibility = Visibility.Visible; 

    StorageFolder storageFolder = KnownFolders.PicturesLibrary; 

??? not sure : 
    var imagefile = await KnownFolders.PicturesLibrary.CreateFileAsync("MyPhoto.jpeg", CreationCollisionOption.ReplaceExisting); 

    photo.CopyAsync(storageFolder,imagefile); 

    } 

回答

1

你在這裏。您必須在清單中設置相機和圖片庫訪問功能。

CameraCaptureUI camera = new CameraCaptureUI(); 
camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9); 
StorageFile photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo); 

//By default the photo will be stored at location 
//%localappdata%\Packages\APP_PACKAGE_ID\TempState 

if (photo != null) 
{ 
    //await photo.MoveAsync(KnownFolders.PicturesLibrary); 
    //OR 
    await photo.MoveAsync(KnownFolders.PicturesLibrary, "DesiredPhotoName" + photo.FileType, NameCollisionOption.GenerateUniqueName); 
} 
+0

它正在工作。這張照片是由照相機定義爲照片的文件類型是storageFile,我沒有定義它的類型。這也適用:await photo.MoveAsync(KnownFolders.PicturesLibrary,「MyPhoto」+「.jpeg」,NameCollisionOption.GenerateUniqueName); DesiredPhotoName的意思是「Name」+「.jpeg」謝謝 – MilkBottle

+0

最好使用'photo.FileType',因爲某些相機可能會提供不同的格式。所以我們不應該_hardcode_格式。 – Xyroid

+0

謝謝。但是,我怎麼知道照相機定義的照片文件類型?在哪裏設置照片文件類型?我認爲它是預定義的,如JPG。 Thans – MilkBottle

相關問題