Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

SL3: Programmatically Take and Save Screenshot? RSS

21 replies

Last post Jun 06, 2010 08:04 AM by jenifferhomes

(0)
  • JeffWeber

    JeffWeber

    Member

    144 Points

    95 Posts

    SL3: Programmatically Take and Save Screenshot?

    Mar 20, 2009 09:32 PM | LINK

    Does anyone know, with the new Bitmap read/write functionality, whether or not it is now possible to programmatically take a screenshot of whatever is on a canvas and allow a user to save it using the new Save dialog?

     Just curious. Haven't had the chance to try it myself yet.

     -Jeff

     

  • .netdan

    .netdan

    Contributor

    3645 Points

    547 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 21, 2009 01:18 AM | LINK

    Hi Jeff,

    In SL3 you can use the following to get and image from a Canvas:

    WriteableBitmap screenshot= new WriteableBitmap((int)myCanvas.RenderSize.Width, (int) myCanvas.RenderSize.Height, PixelFormats.Bgr32);

    screenshot.Render(myCanvas, new TranslateTransform());

    I have put together a quick tutorial showing how to render a UI Element to a Bitmap.

    silverlight 3 write bitmap

    Remember to click "Answer" if this has helped you!

    Dan Birch
    MCSD
    Free Silverlight,HTML5 Games, Controls and CMS | Free HTML5 iPhone, Android Games
  • Qbus

    Qbus

    Member

    611 Points

    271 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 21, 2009 10:35 AM | LINK

    I dunno if you have seen it, but Joe Stegman had a session at MIX09 where he shows how to use this new API.

    http://videos.visitmix.com/MIX09/T14F

    The session includes a lot of other good stuff thats new in SL3, but he also shows how to use this API to save thumbnails of a video by clicking it :)

     

    -Enjoy!

    --------------------------
    Please mark the post as answered if this answers your question
    http://www.laumania.net
  • JeffWeber

    JeffWeber

    Member

    144 Points

    95 Posts

    Re: Re: SL3: Programmatically Take and Save Screenshot?

    Mar 21, 2009 11:18 AM | LINK

    Ok, cool. Thanks guys...
  • ksleung

    ksleung

    Contributor

    6680 Points

    1265 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 24, 2009 12:14 AM | LINK

    I have a follow-up question.   Yes we can take a snapshot of an existing control (image, video, layout, etc) and render it to a WriteableBitmap to be displayed as an image.  However, it seems that the WriteableBitmap will then be unreadable (after the Render() call) other than to be used as an ImageSource -- reading it will trigger an exception once Render() is called.  Does anyone have an example how to access the content of the WriteableBitmap?

    It was mentioned by various folks that the WriteableBitmap feature can be used to do various great things, such as red-eye correction, but that requires access to the content of the bitmap.  I have asked a few MSFT folks this question but I haven't got any answer yet.  So far all the WriteableBitmap examples I have seen showed how to render something else into a bitmap which is to be immediately consumed as an ImageSource.  No example shows how to gain access to the content of the Bitmap for the purpose of manipulation.  If security violation is the reasoning behind, well, I don't seem to find any documentation about that either.

    If what I said cannot be done, we won't be able to save the Bitmap to a file either.

    Any idea any one?

    JeffWeber

    Does anyone know, with the new Bitmap read/write functionality, whether or not it is now possible to programmatically take a screenshot of whatever is on a canvas and allow a user to save it using the new Save dialog?

     Just curious. Haven't had the chance to try it myself yet.

     -Jeff

     

     
    Visit http://www.tagxedo.com, a Silverlight-based word cloud generator. If you like it, please help me spread the word!
  • abeaulieu

    abeaulieu

    Participant

    798 Points

    132 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 24, 2009 01:22 AM | LINK

    Yes, in SL3 Beta you are not allowed to read the Pixels after a Render call, for security reasons. This may or may not be fixed in RTM. More on that in this blog post.

    Believe me, I put it high on my wish list for RTM, it is very useful for pixel-level collision detection!

     

    Andy Beaulieu
    www.andybeaulieu.com

    If your question was answered, please mark the response as the answer.
  • ksleung

    ksleung

    Contributor

    6680 Points

    1265 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 24, 2009 05:47 AM | LINK

    Andy,

    Thanks for the tips.  I certainly hope that this will be addressed in RTM!  Certainly, without the ability to read a rendered bitmap it is hard to do many of the interesting things one would expect from the new bitmap feature.  Anyway, I'll need to live with this for the time being.

    Since you are in the know, can I ask you one more question?  Pixel Shader is another great feature in SL3, but so far I have only seen examples where you directly apply the effect to images, videos, or controls.  There does not seem to be any API I can use to examine the result of applying Pixel Shader to a WriteableBitmap (for the sake of argument let's say such bitmap is not generated by rendering so there is no security concern).  In other words, is it possible to use Pixel Shader directly as a poor man's SIMD engine?  I know Pixel Shader is not GPU-accelerated but still it beats pixel-by-pixel direct manipulation through array indexing.  Do you know whether this is possible?  Let me know if you have more insight in this.  Thanks!

    abeaulieu

    Yes, in SL3 Beta you are not allowed to read the Pixels after a Render call, for security reasons. This may or may not be fixed in RTM. More on that in this blog post.

    Believe me, I put it high on my wish list for RTM, it is very useful for pixel-level collision detection!

     

    WriteableBitmap

    Visit http://www.tagxedo.com, a Silverlight-based word cloud generator. If you like it, please help me spread the word!
  • abeaulieu

    abeaulieu

    Participant

    798 Points

    132 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 24, 2009 11:49 AM | LINK

    From what I understand, they (the Silverlight team) did not have time to implement any security on WriteableBitmap, so they basically just shut _all_ pixel access off for Beta. Hopefully by RTM we will be able to read Pixels if the WriteableBitmap source was from the domain of origin, a resource, or a Render method on a local resource.  

    I hadn't thought of the situation you suggest - that's a good idea. I haven't tried this, but I have to assume you could add an Effect to a UI Element, and then use WriteableBitmap to Render that UI Element to a Bitmap. And then (if RTM has Pixel access), read the pixel values from the WB.

    Andy Beaulieu
    www.andybeaulieu.com

    If your question was answered, please mark the response as the answer.
  • ksleung

    ksleung

    Contributor

    6680 Points

    1265 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 24, 2009 04:16 PM | LINK

    That's right.  Being able to render an image with effect to a WriteableBitmap and then read from it (assuming the RTM pixel access) would achieve the effect I described, although something more direct would be even better.

    I actually don't understand the security thing.  Is it conceptually okay if the image is generated from a stream, but not okay if the image is created directly with a given external URI?  I thought it does not make sense to disallow an app (even a rogue app) from seeing what it can otherwise see but perhaps more indirectly...  For example, to see the content of a image downloaded from a URI it can simply parse the image itself (using FJCore Jpeg decoder).  I guess I am missing something.  Anyway, perhaps this is getting off topic...

    Thanks for your clarification, Andy.

    Also, Jeff, hope you don't mind the digression from your thread.  I have a need similar to yours.  From this discussion, the answer to your question is currently NO.  Hopefully it'll become YES in RTM.

    abeaulieu

    From what I understand, they (the Silverlight team) did not have time to implement any security on WriteableBitmap, so they basically just shut _all_ pixel access off for Beta. Hopefully by RTM we will be able to read Pixels if the WriteableBitmap source was from the domain of origin, a resource, or a Render method on a local resource.  

    I hadn't thought of the situation you suggest - that's a good idea. I haven't tried this, but I have to assume you could add an Effect to a UI Element, and then use WriteableBitmap to Render that UI Element to a Bitmap. And then (if RTM has Pixel access), read the pixel values from the WB.

     
    Visit http://www.tagxedo.com, a Silverlight-based word cloud generator. If you like it, please help me spread the word!
  • ksleung

    ksleung

    Contributor

    6680 Points

    1265 Posts

    Re: SL3: Programmatically Take and Save Screenshot?

    Mar 24, 2009 04:20 PM | LINK

    Clarification to the last paragraph.  NO as in you can take the snapshot but you will not be able to save it.

    ksleung

    That's right.  Being able to render an image with effect to a WriteableBitmap and then read from it (assuming the RTM pixel access) would achieve the effect I described, although something more direct would be even better.

    I actually don't understand the security thing.  Is it conceptually okay if the image is generated from a stream, but not okay if the image is created directly with a given external URI?  I thought it does not make sense to disallow an app (even a rogue app) from seeing what it can otherwise see but perhaps more indirectly...  For example, to see the content of a image downloaded from a URI it can simply parse the image itself (using FJCore Jpeg decoder).  I guess I am missing something.  Anyway, perhaps this is getting off topic...

    Thanks for your clarification, Andy.

    Also, Jeff, hope you don't mind the digression from your thread.  I have a need similar to yours.  From this discussion, the answer to your question is currently NO.  Hopefully it'll become YES in RTM.

     
    Visit http://www.tagxedo.com, a Silverlight-based word cloud generator. If you like it, please help me spread the word!