locked
how to create a PrintScreen virtual key click with a button and save the image

    Question

  • Hi,

    I'm trying to create a virtual key click (PrintScreen) with a UI button ("Capture") in Metro style app by C#.

    and save the image as bitmap or to StorageFile.

    How to create a event to do that? 

    I'm trying to use PrintScreen, but I only found enum VirtualKey.Snapshot under Windows.System, but I don't know how to use it.

    Or is there any API or method I can use directly to save PrintScreen image?

    I found something VS forum, but it only works for .NET

    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/79efecc4-fa6d-4078-afe4-bb1379bb968b

    using System.Drawing;  
    using System.Drawing.Imaging; 
    
    private void PrintScreen()
    
    {  
    
        Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    
        Graphics graphics = Graphics.FromImage(printscreen as Image);
    
        graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
    
        printscreen.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);
    
    }

    Thanks in advance,


    Jerry




    • Edited by Jerrywu12 Monday, May 28, 2012 9:03 AM
    Monday, May 28, 2012 8:39 AM

Answers

  • Hi Jerry,

    There is no way to programmatically capture a screenshot of a Metro style app in Xaml. Metro style apps cannot synthesize input and so cannot fake the PrintScreen key, they cannot access the screen buffer like your code snippet does, and there is no way to render a Xaml element to a bitmap.

    Tuesday, May 29, 2012 2:50 AM
    Moderator
  • There is no way to render a Xaml element (including a Canvas) into a bitmap. Depending on what exactly you are working with, you may be able to save off the essential properties of the elements within your Canvas as a vector file and then recreate them when you read that back in.

    If you need to be able to save out a bitmap then you will need to do your drawing at a pixel level with WriteableBitmap. There are some 3rd party drawing libraries coming (or possibly already available) which can help draw higher level items than just points. This won't work for Xaml controls though: only for drawings.

    --Rob

    Tuesday, May 29, 2012 5:04 PM
    Moderator

All replies

  • Hi Jerry,

    There is no way to programmatically capture a screenshot of a Metro style app in Xaml. Metro style apps cannot synthesize input and so cannot fake the PrintScreen key, they cannot access the screen buffer like your code snippet does, and there is no way to render a Xaml element to a bitmap.

    Tuesday, May 29, 2012 2:50 AM
    Moderator
  • Thanks Rob,

    but is there a way to save the drawing on Canvas as any other form of image or file? since it won't work as bitmap image.

    Thanks,


    Jerry


    • Edited by Jerrywu12 Tuesday, May 29, 2012 8:51 AM
    Tuesday, May 29, 2012 8:03 AM
  • There is no way to render a Xaml element (including a Canvas) into a bitmap. Depending on what exactly you are working with, you may be able to save off the essential properties of the elements within your Canvas as a vector file and then recreate them when you read that back in.

    If you need to be able to save out a bitmap then you will need to do your drawing at a pixel level with WriteableBitmap. There are some 3rd party drawing libraries coming (or possibly already available) which can help draw higher level items than just points. This won't work for Xaml controls though: only for drawings.

    --Rob

    Tuesday, May 29, 2012 5:04 PM
    Moderator