Convert CanvasRenderTarget to ImageSource in WinUI

Posted by: canoe.company-0a on 3 November 2022, 1:02 am EST

    • Post Options:
    • Link

    Posted 3 November 2022, 1:02 am EST

    I’m working on .Net MAUI and I used native rendering in my application. So for Windows (WinUi-3), I have drawings rendered on the CanvasRenderTarget object and now I have to provide this as ImageSource(MAUI) to the user.

    I tried to get Pixels from canvasrendertarget object and later to the stream source. But setting this stream source to the Image control, I’m not able to get the image output.

             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();
    
            //Setting the Stream source to my Image control
            imageview.Source = ImageSource.FromStream(() => new MemoryStream(bytes));
    

    Not sure what I’m missing, how can I convert WinUi RenderTarget object to MAUI.ImageSource ?

  • Posted 3 November 2022, 11:50 pm EST

    Hi,

    It seems like your query is not related to our product.

    JFYI, this is a product specific forum and you can find your answer here : https://stackoverflow.com/questions/71926336/convert-canvasrendertarget-to-imagesource-in-winui.

    Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels