locked
Calling RenderAsync for Background RenderTargetBitmap didn't return after 15 seconds.

    Question

  • I wanna generate image in the background with RenderTargetBitmap.

    I created CoreApplicationview with CoreaApplication.CreateNewView as background UI thread and tried to generate image on this thread by calling RenderTargetBitmap.RenderAsync for target UIElement.

    This works successfully for 15 seconds,but after that calling RenderAsync didn't return.

    Is there some limitation?Or is there some good way to improve this?

    I put repro code below.

    If you run this app,you can see two button.

    Clicking right button start generating image on background UI thread.You can see some trace in Output window.

    Images are generated in the LocalState folder of app packages.

    repro code is here


    cyclops



    • Edited by cyclops2 Friday, March 21, 2014 8:14 AM
    Friday, March 21, 2014 8:03 AM

All replies

  • What do you mean by "it works for 15 seconds"?

    WinRT doesn't allow you to work with UIElements on background threads.  You would have to do any rendering on the UI thread by calling into the Dispatcher for the UI thread.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 21, 2014 2:23 PM
    Moderator
  • "Background" means I do something on new CoreApplicationView instead of main CoreApplicationView.

    I plan to pass some data from main CoreaApplicationView to new CoreApplicationView,then render image with those data on new CoreApplicationView.

    In my repro code,as I told above,I create new CoreApplicationView,then I start DispatcherTimer on that CoreAplicationView to render canvas and save to file every 1 sec.

    It continues generate image file for 15 seconds,but after that,RenderAsync don't return,so no more image files are generated.


    cyclops


    • Edited by cyclops2 Friday, March 21, 2014 3:09 PM
    Friday, March 21, 2014 2:36 PM
  • I might be wrong but accoding to me it really works fine :)

    private async void Snapshot_Click(object sender, RoutedEventArgs e)
    {    await CaptureAndSave(this);}async System.Threading.Tasks.Task<Windows.Storage.StorageFile> CaptureAndSave(UIElement element)
    {    // capture    var bitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();    await bitmap.RenderAsync(element);    var picker = new Windows.Storage.Pickers.FileSavePicker    {        SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary,        FileTypeChoices = { { "JPEG", new[] { ".jpg" } } },        SuggestedFileName = "Image",        DefaultFileExtension = ".jpg",        CommitButtonText = "Save",    };    // save    var file = await picker.PickSaveFileAsync();    using (var stream = await file.OpenStreamForWriteAsync())    {        var pixel = await bitmap.GetPixelsAsync();        var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;        var random = stream.AsRandomAccessStream();        var jpg = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;        var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(jpg, random);        var format = Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8;        var alpha = Windows.Graphics.Imaging.BitmapAlphaMode.Ignore;        var width = (uint)bitmap.PixelWidth;        var height = (uint)bitmap.PixelHeight;        encoder.SetPixelData(format, alpha, width, height, dpi, dpi, pixel.ToArray());        await encoder.FlushAsync();    }    return file;}


    Microsoft Student Partner

    • Proposed as answer by Bluxo Costa Wednesday, April 9, 2014 9:56 PM
    • Unproposed as answer by cyclops2 Thursday, April 10, 2014 4:23 AM
    Saturday, April 5, 2014 9:28 PM
  • Thx for your reply.

    I can render bitmap fine with RenderTargetBitmap on main thread(CoreApplicationView) too.

    My question is why calling RenderAsync of RenderTargetBitmap don't return 15 seconds after on "background"(calling like this may be incorrect...) CoreApplicationView.


    cyclops

    • Proposed as answer by Bluxo Costa Wednesday, April 9, 2014 9:56 PM
    • Unproposed as answer by cyclops2 Wednesday, April 9, 2014 10:12 PM
    Sunday, April 6, 2014 11:53 PM