I'm new in C# coding and I would like to save a video frame in Jpeg-XR (16-bits image). I wrote some lines of code that should be close to the solution but I'm only able to correctly save a 8-bits image.
//Use Windows.Media.Editing to get ImageStream
var clip = await MediaClip.CreateFromFileAsync(file);
var composition = new MediaComposition();
composition.Clips.Add(clip);
var imageStream = await composition.GetThumbnailAsync(Position, (int)frameWidth, (int)frameHeight, VideoFramePrecision.NearestFrame);
//generate bitmap
var writableBitmap = new WriteableBitmap((int)frameWidth, (int)frameHeight);
writableBitmap.SetSource(imageStream);
//generate some random name for file in PicturesLibrary
var name = Position.Minutes.ToString() + "_" + Position.Seconds.ToString() + "_"+ Position.Milliseconds.ToString();
var saveAsTarget = await KnownFolders.PicturesLibrary.CreateFileAsync(name + file.DisplayName + ".jxr");
//get stream from bitmap
const int BytesPerPixel = 4;
Stream stream = writableBitmap.PixelBuffer.AsStream();
byte[] pixels = new byte[(int)frameWidth * (int)frameHeight * BytesPerPixel];
await stream.ReadAsync(pixels, 0, pixels.Length);
using (var writeStream = await saveAsTarget.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegXREncoderId, writeStream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)writableBitmap.PixelWidth,
(uint)writableBitmap.PixelHeight,
96.0,
96.0,
pixels);
await encoder.FlushAsync();
using (var outputStream = writeStream.GetOutputStreamAt(0))
{
await outputStream.FlushAsync();
}
}
I tried to set the BitmapPixelFormat to Rgba16 but the results is a wrongly encoded image. Any clue on how to solve my problems?
Alternative solutions to my current code are welcome. Thank you :)