// </SnippetReadImageMetadata> // <SnippetWriteImageMetadata> private async void WriteImageMetadata(BitmapEncoder bitmapEncoder) { var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet(); var orientationValue = new Windows.Graphics.Imaging.BitmapTypedValue( 1, // Defined as EXIF orientation = "normal" Windows.Foundation.PropertyType.UInt16 ); propertySet.Add("System.Photo.Orientation", orientationValue); try { await bitmapEncoder.BitmapProperties.SetPropertiesAsync(propertySet); } catch (Exception err) { switch (err.HResult) { case unchecked ((int)0x88982F41): // WINCODEC_ERR_PROPERTYNOTSUPPORTED // The file format does not support this property. break; default: throw err; } } }
private async void WriteImageMetadata(BitmapEncoder bitmapEncoder, BitmapPropertiesView inputProperties) { var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet(); var requests = new System.Collections.Generic.List <string>(); requests.Add("System.Photo.Orientation"); var retrievedProps = await inputProperties.GetPropertiesAsync(requests); if (retrievedProps.ContainsKey("System.Photo.Orientation")) { propertySet.Add("System.Photo.Orientation", retrievedProps["System.Photo.Orientation"]); } else { var orientationValue = new Windows.Graphics.Imaging.BitmapTypedValue( 1, // Defined as EXIF orientation = "normal" Windows.Foundation.PropertyType.UInt16 ); propertySet.Add("System.Photo.Orientation", orientationValue); } if (date.Date != null) { var datetaken = new Windows.Graphics.Imaging.BitmapTypedValue( date.Date, Windows.Foundation.PropertyType.DateTime ); propertySet.Add("System.Photo.DateTaken", datetaken); } var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue( 1.0, // Maximum quality Windows.Foundation.PropertyType.Single ); var location = new Windows.Graphics.Imaging.BitmapTypedValue( 1.0, // Maximum quality Windows.Foundation.PropertyType.Single ); propertySet.Add("ImageQuality", qualityValue); try { await bitmapEncoder.BitmapProperties.SetPropertiesAsync(propertySet); } catch (Exception err) { switch (err.HResult) { case unchecked ((int)0x88982F41): // WINCODEC_ERR_PROPERTYNOTSUPPORTED // The file format does not support the requested metadata. break; case unchecked ((int)0x88982F81): // WINCODEC_ERR_UNSUPPORTEDOPERATION // The file format does not support any metadata. break; } } }
public static async Task WriteableBitmapToStorageFile(WriteableBitmap WB, FileFormat fileFormat, int compression, StorageFile file) { string FileName = "YourFile."; Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId; switch (fileFormat) { case FileFormat.Jpeg: FileName += "jpeg"; BitmapEncoderGuid = BitmapEncoder.JpegEncoderId; break; case FileFormat.Png: FileName += "png"; BitmapEncoderGuid = BitmapEncoder.PngEncoderId; break; case FileFormat.Bmp: FileName += "bmp"; BitmapEncoderGuid = BitmapEncoder.BmpEncoderId; break; case FileFormat.Tiff: FileName += "tiff"; BitmapEncoderGuid = BitmapEncoder.TiffEncoderId; break; case FileFormat.Gif: FileName += "gif"; BitmapEncoderGuid = BitmapEncoder.GifEncoderId; break; } using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet(); var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue( 1 - (compression / 100), Windows.Foundation.PropertyType.Single ); propertySet.Add("ImageQuality", qualityValue); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream, propertySet); Stream pixelStream = WB.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)WB.PixelWidth, (uint)WB.PixelHeight, 96.0, 96.0, pixels); await encoder.FlushAsync(); } }
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); }
// </SnippetSaveSoftwareBitmapToFile> private async void UseEncodingOptions(StorageFile outputFile) { using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) { // <SnippetUseEncodingOptions> var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet(); var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue( 1.0, // Maximum quality Windows.Foundation.PropertyType.Single ); propertySet.Add("ImageQuality", qualityValue); await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync( Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, stream, propertySet ); // </SnippetUseEncodingOptions> } }
public async Task CaptureFrame() { try { using (var ms = new InMemoryRandomAccessStream()) { SoftwareBitmap bitmap = null; var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet(); var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue( 0.5, // Quality percentage. Windows.Foundation.PropertyType.Single ); propertySet.Add("ImageQuality", qualityValue); var frameReference = this.CameraFrameReader.TryAcquireLatestFrame(); var frame = frameReference?.VideoMediaFrame?.SoftwareBitmap; this.CameraFrameReader.AcquisitionMode = MediaFrameReaderAcquisitionMode.Realtime; if (frame == null) { var surface = frameReference?.VideoMediaFrame?.Direct3DSurface; if (surface != null) { bitmap = await SoftwareBitmap.CreateCopyFromSurfaceAsync(surface, BitmapAlphaMode.Ignore).AsTask().ConfigureAwait(false); // JPEG Encoder no likey YUY2. if (bitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8) { bitmap = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore); } } } else { bitmap = SoftwareBitmap.Convert(frame, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore); } if (bitmap == null) { return; } using (bitmap) { var jpegEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms, propertySet).AsTask().ConfigureAwait(false); jpegEncoder.SetSoftwareBitmap(bitmap); // No thumbnail for just "streaming". //jpegEncoder.IsThumbnailGenerated = false; try { await jpegEncoder.FlushAsync().AsTask().ConfigureAwait(false); } catch (Exception err) { switch (err.HResult) { case unchecked ((int)0x88982F81): //WINCODEC_ERR_UNSUPPORTEDOPERATION // If the encoder does not support writing a thumbnail, then try again // but disable thumbnail generation. jpegEncoder.IsThumbnailGenerated = false; break; default: throw err; } } if (ms.Size > 0) { // TODO: make this more efficient. byte[] data = (byte[])Array.CreateInstance(typeof(byte), (int)ms.Size); ms.AsStreamForRead().Read(data, 0, (int)ms.Size); this.CapturedFrame = new Frame() { Data = data, Format = "jpeg", Width = (uint)bitmap.PixelWidth, Height = (uint)bitmap.PixelHeight }; } } } } catch (Exception e) { this.Log.Error(() => "Exception while capturing frame.", e); } }
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 { inputStream = await tempStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read); var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(inputStream); photoStorage = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME, 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; }