我有3个按钮和1个图像视图。第一个按钮(相机)打开相机并单击图像并将单击的图像设置为该图像视图。第二个按钮(文件)拍摄文件夹中的图像,我想要我的第三个按钮(保存)将显示的图像保存到手机中。在桌面设备中,我可以轻松使用文件选择器选项将图像存储在受尊重的文件夹中,但我想在移动图库中按下该按钮后自动保存图像。
答案 0 :(得分:1)
基于code.msdn上的样本 而不是显示第一个存储文件的内容,然后创建一个新文件以保存文件我只是将代码示例与图片文件夹中随机生成的文件名组合在一起(记得添加图片文件夹功能。)
private StorageFile storeFile;
private IRandomAccessStream stream;
private async void SavePhotoClicked(object sender, RoutedEventArgs e)
{
try
{
CameraCaptureUI capture = new CameraCaptureUI();
capture.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
capture.PhotoSettings.CroppedAspectRatio = new Size(3, 5);
capture.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;
storeFile = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (storeFile != null)
{
stream = await storeFile.OpenAsync(FileAccessMode.Read);
var s = await KnownFolders.PicturesLibrary.CreateFileAsync(DateTime.UtcNow.ToString("yyyyMMddHHmmss") + ".jpg");
using (var dataReader = new DataReader(stream.GetInputStreamAt(0)))
{
await dataReader.LoadAsync((uint)stream.Size);
byte[] buffer = new byte[(int)stream.Size];
dataReader.ReadBytes(buffer);
await FileIO.WriteBytesAsync(s, buffer);
}
}
}
catch (Exception ex)
{
var messageDialog = new MessageDialog(ex.Message, "Unable to save now.");
await messageDialog.ShowAsync();
}
}