locked
Problem with RenderTargetBitmap !

    Question

  • I'm attempting to get the bitmap of a Canvas using RenderTargetBitmap and Nokia Imaging SDK. But getting Invalid Argument Exception

    Here is the code:

    RenderTargetBitmap UIBitmap = new RenderTargetBitmap();
    await UIBitmap.RenderAsync(AnnotationsCanvas);
    var pixelBuffer = await UIBitmap.GetPixelsAsync();
    
    var annotationsBitmap = new Bitmap(new Windows.Foundation.Size((int)(AnnotationsCanvas.ActualWidth), (int)(AnnotationsCanvas.ActualHeight)), ColorMode.Bgra8888);
    
    using (var annSource = new BufferImageSource(pixelBuffer))
    using (var renderer = new BitmapRenderer(annSource, annotationsBitmap))
        {
            await renderer.RenderAsync();
        }

    Am I doing it in a right way?


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Thursday, June 12, 2014 4:34 PM

Answers

  • That's not a problem with the RenderTargetBitmap. Use that as your ImageSource and it should work fine.

    The problem is in your WriteableBitmap handling. SetSource expects a stream to an image file, not to a pixel buffer. See the SetSourceAsync method documentation and remarks. To access the WriteableBitmap's pixel buffer use its PixelBuffer property

    --Rob

    • Marked as answer by venu238 Thursday, June 12, 2014 8:24 PM
    Thursday, June 12, 2014 7:27 PM
    Owner

All replies

  • Where are you getting the error?

    The RenderTargetBitmap code looks correct. I'm not familiar enough with the Nokia Imaging SDK to comment on that part.

    --Rob

    Thursday, June 12, 2014 5:00 PM
    Owner
  • getting error while rendereing the bitmap at

    await renderer.RenderAsync();

    This is the exception I'm getting 

    An exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll but was not handled in user code

    Additional information: Value does not fall within the expected range.


    Clipr : Best ever clipping tool in Windows Phone!
    Download here


    • Edited by venu238 Thursday, June 12, 2014 5:36 PM
    Thursday, June 12, 2014 5:32 PM
  • BitmapRenderer isn't a Windows component. I assume it's from the Nokia Imaging SDK. Please ask questions about that SDK at Discussion: Windows Phone Imaging
    Thursday, June 12, 2014 6:21 PM
    Owner
  • Okay I've also problem without using Nokia Imaging SDK..Check below code

    RenderTargetBitmap UIBitmap = new RenderTargetBitmap();
    await UIBitmap.RenderAsync(AnnotationsCanvas);
    var pixelBuffer = await UIBitmap.GetPixelsAsync();
                        
    
    var annotationsBitmap = new WriteableBitmap((int)(UIBitmap.PixelWidth), (int)(UIBitmap.PixelHeight));
    annotationsBitmap.SetSource(pixelBuffer.AsStream().AsRandomAccessStream());

    I'm not understanding why the above code is not working. It's not even giving any error after stepping into annotationBitmap.SetSource()..


    Clipr : Best ever clipping tool in Windows Phone!
    Download here


    • Edited by venu238 Thursday, June 12, 2014 7:12 PM
    Thursday, June 12, 2014 7:00 PM
  • That's not a problem with the RenderTargetBitmap. Use that as your ImageSource and it should work fine.

    The problem is in your WriteableBitmap handling. SetSource expects a stream to an image file, not to a pixel buffer. See the SetSourceAsync method documentation and remarks. To access the WriteableBitmap's pixel buffer use its PixelBuffer property

    --Rob

    • Marked as answer by venu238 Thursday, June 12, 2014 8:24 PM
    Thursday, June 12, 2014 7:27 PM
    Owner
  • Uffff ! Eventually everything is working out smooth.

    Thanks for the help Rob :)

    Below is the code to convert UIElement to WriteableBitmap 

    RenderTargetBitmap UIBitmap = new RenderTargetBitmap();
    await UIBitmap.RenderAsync(UIElement);
    var pixelBuffer = await UIBitmap.GetPixelsAsync();
    byte[] pixels = pixelBuffer.ToArray();
    
    var writeableBitmap = new WriteableBitmap((int)UIBitmap.PixelWidth, (int)UIBitmap.PixelHeight);
    
    using (Stream stream = writeableBitmap .PixelBuffer.AsStream())
    {
         await stream.WriteAsync(pixels, 0, pixels.Length);
    }


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Thursday, June 12, 2014 8:24 PM