private async Task <Windows.Storage.StorageFile> ReencodePhotoAsync(Windows.Storage.StorageFile tempStorageFile, Windows.Storage.FileProperties.PhotoOrientation photoRotation) { Windows.Storage.Streams.IRandomAccessStream inputStream = null; Windows.Storage.Streams.IRandomAccessStream outputStream = null; Windows.Storage.StorageFile photoStorage = null; try { String newPhotoFilename = photoId + "_" + photoCount.ToString() + ".jpg"; inputStream = await tempStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read); var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(inputStream); photoStorage = await currentFolder.CreateFileAsync(newPhotoFilename, Windows.Storage.CreationCollisionOption.GenerateUniqueName); outputStream = await photoStorage.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); outputStream.Size = 0; var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder); var properties = new Windows.Graphics.Imaging.BitmapPropertySet(); properties.Add("System.Photo.Orientation", new Windows.Graphics.Imaging.BitmapTypedValue(photoRotation, Windows.Foundation.PropertyType.UInt16)); await encoder.BitmapProperties.SetPropertiesAsync(properties); await encoder.FlushAsync(); } finally { if (inputStream != null) { inputStream.Dispose(); } if (outputStream != null) { outputStream.Dispose(); } var asyncAction = tempStorageFile.DeleteAsync(Windows.Storage.StorageDeleteOption.PermanentDelete); } return(photoStorage); }
async void Save(Stream stream, string filename, string type) { stream.Position = 0; StorageFile stFile; if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))) { FileSavePicker savePicker = new FileSavePicker(); savePicker.DefaultFileExtension = type; savePicker.SuggestedFileName = filename.Substring(0, filename.IndexOf(".")); if (type == ".pdf") { savePicker.FileTypeChoices.Add("Adobe PDF Document", new List <string>() { type }); } else { savePicker.FileTypeChoices.Add("JPG", new List <string>() { type }); } stFile = await savePicker.PickSaveFileAsync(); } else { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); } if (stFile != null) { Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite); Stream st = fileStream.AsStreamForWrite(); st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length); st.Flush(); st.Dispose(); fileStream.Dispose(); } }
public void Dispose() { internalStream.Dispose(); }