I'm working on a new .Net MAUI App, where I need to render signature points on the native platform. So for Windows which is WinUI, I need to convert the UI to MAUI ImageSource to display in a image control. For this, I have tried converting canvasrendertarget to byte array, and then to a Stream image source.
But the issue I'm facing was, while converting this image to a image control, navigating second only the image get updated. Also some of the color are updated wrongly that for black it is working fine. For orange it rendered as blue.
var renderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), (float)400, (float)400, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.FillCircle(10, 10, 400, Windows.UI.Color.FromArgb(250, 140, 120, 150));
}
byte[] bytes = renderTarget.GetPixelBytes();
var stream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Rgba8 , BitmapAlphaMode.Straight, 400, 400, 96, 96, bytes);
await encoder.FlushAsync();
//Setting the Stream source to my Image control imageview.Source = ImageSource.FromStream(() => stream.AsStream());
Thanks in advance. Also I'm new to WinUi and let me know how can I add asnyc to the Image control because navigating second time alone works fine.