/// <summary> /// Utility method to convert the MIME type enumeration used by this class to the /// platform's Bitmap Encoder GUID. /// </summary> /// <param name="mimeType">MIME type to convert.</param> /// <returns>GUID that can be used in the bitmap encoder to encode this MIME type.</returns> public static Guid GetBitmapEncoderIdForMimeType(ImageMimeType mimeType) { switch (mimeType) { case ImageMimeType.Png: return(BitmapEncoder.PngEncoderId); case ImageMimeType.Jpg: case ImageMimeType.Jpeg: return(BitmapEncoder.JpegEncoderId); case ImageMimeType.JpegXr: return(BitmapEncoder.JpegXREncoderId); case ImageMimeType.Bmp: return(BitmapEncoder.BmpEncoderId); case ImageMimeType.Gif: return(BitmapEncoder.GifEncoderId); case ImageMimeType.Tiff: return(BitmapEncoder.TiffEncoderId); default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Set an image to this class by specifying the WritableBitmap and the target encoding and /// optionally the DPI to set for the target file (if supported by the target file type). /// </summary> /// <remarks> /// The method takes the bitmap as input and uses the BitmapEncoder to encode /// the image data into the specified MIME type and file format. The encoded contents are /// then stored to the payload of the record, the MIME type is set as the type of the record. /// </remarks> /// <param name="bmp">Input bitmap to encode and set as payload of this record.</param> /// <param name="mimeType">MIME type to use for encoding this image.</param> /// <param name="dpi">Target DPI if supported by the target file / MIME format.</param> /// <returns>Task to await completion of the asynchronous image encoding.</returns> public async Task SetImage(WriteableBitmap bmp, ImageMimeType mimeType, double dpi = 96.0) { var encoderId = GetBitmapEncoderIdForMimeType(mimeType); byte[] pixels; using (var stream = bmp.PixelBuffer.AsStream()) { pixels = new byte[(uint)stream.Length]; await stream.ReadAsync(pixels, 0, pixels.Length); } using (var imgStream = new MemoryStream()) { var raImgStream = imgStream.AsRandomAccessStream(); var encoder = await BitmapEncoder.CreateAsync(encoderId, raImgStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)bmp.PixelWidth, (uint)bmp.PixelHeight, dpi, dpi, pixels); await encoder.FlushAsync(); await raImgStream.FlushAsync(); Payload = imgStream.ToArray(); Type = ImageMimeTypes[mimeType]; } }
/// <summary> /// Construct a new MIME / Image record based on a WriteableBitmap. /// Specify the MIME type and DPI, and the image data is encoded accordingly. /// </summary> /// <param name="bmp">Source bitmap to use for conversion.</param> /// <param name="mimeType">MIME type to specify the target image encoding.</param> /// <param name="dpi">Optional parameter to set the DPI of the encoded image /// (if supported by the specific MIME type).</param> /// <returns>A newly constructed MIME / Image record with the WriteableBitmap /// encoded into the specified MIME type.</returns> public static async Task <NdefMimeImageRecord> CreateFromBitmap(WriteableBitmap bmp, ImageMimeType mimeType, double dpi = 96.0) { var imgRecord = new NdefMimeImageRecord(); await imgRecord.SetImage(bmp, mimeType, dpi); return(imgRecord); }
/// <summary> /// Construct a new MIME / Image record based on a WriteableBitmap. /// Specify the MIME type and DPI, and the image data is encoded accordingly. /// </summary> /// <param name="bmp">Source bitmap to use for conversion.</param> /// <param name="mimeType">MIME type to specify the target image encoding.</param> /// <param name="dpi">Optional parameter to set the DPI of the encoded image /// (if supported by the specific MIME type).</param> /// <returns>A newly constructed MIME / Image record with the WriteableBitmap /// encoded into the specified MIME type.</returns> public static async Task<NdefMimeImageRecord> CreateFromBitmap(WriteableBitmap bmp, ImageMimeType mimeType, double dpi = 96.0) { var imgRecord = new NdefMimeImageRecord(); await imgRecord.SetImage(bmp, mimeType, dpi); return imgRecord; }
/// <summary> /// Utility method to convert the MIME type enumeration used by this class to the /// platform's Bitmap Encoder GUID. /// </summary> /// <param name="mimeType">MIME type to convert.</param> /// <returns>GUID that can be used in the bitmap encoder to encode this MIME type.</returns> public static Guid GetBitmapEncoderIdForMimeType(ImageMimeType mimeType) { switch (mimeType) { case ImageMimeType.Png: return BitmapEncoder.PngEncoderId; case ImageMimeType.Jpg: case ImageMimeType.Jpeg: return BitmapEncoder.JpegEncoderId; case ImageMimeType.JpegXr: return BitmapEncoder.JpegXREncoderId; case ImageMimeType.Bmp: return BitmapEncoder.BmpEncoderId; case ImageMimeType.Gif: return BitmapEncoder.GifEncoderId; case ImageMimeType.Tiff: return BitmapEncoder.TiffEncoderId; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Set an image to this class by specifying the WritableBitmap and the target encoding and /// optionally the DPI to set for the target file (if supported by the target file type). /// </summary> /// <remarks> /// The method takes the bitmap as input and uses the BitmapEncoder to encode /// the image data into the specified MIME type and file format. The encoded contents are /// then stored to the payload of the record, the MIME type is set as the type of the record. /// </remarks> /// <param name="bmp">Input bitmap to encode and set as payload of this record.</param> /// <param name="mimeType">MIME type to use for encoding this image.</param> /// <param name="dpi">Target DPI if supported by the target file / MIME format.</param> /// <returns>Task to await completion of the asynchronous image encoding.</returns> public async Task SetImage(WriteableBitmap bmp, ImageMimeType mimeType, double dpi = 96.0) { var encoderId = GetBitmapEncoderIdForMimeType(mimeType); byte[] pixels; using (var stream = bmp.PixelBuffer.AsStream()) { pixels = new byte[(uint)stream.Length]; await stream.ReadAsync(pixels, 0, pixels.Length); } using (var imgStream = new MemoryStream()) { var raImgStream = imgStream.AsRandomAccessStream(); var encoder = await BitmapEncoder.CreateAsync(encoderId, raImgStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)bmp.PixelWidth, (uint)bmp.PixelHeight, dpi, dpi, pixels); await encoder.FlushAsync(); await raImgStream.FlushAsync(); Payload = imgStream.ToArray(); Type = ImageMimeTypes[mimeType]; } }