In some instances when working on projects we would require to take a screen capture of the current screen the user is working on. I regularly did this on .NET Compact Framework 3.5 (pre Windows Phone 7) and is a little bit of mess because of all the PInvokes involved.
I decided to create a quick sample app that captures the screen on both platforms to compare the code. The sample app basically just places the captured screen inside a pictureBox control (NETCF 3.5) or Image control (Silverlight for WP7). The outer PictureBox/Image control just displays some standard images that come with Windows 7.
Compact Framework 3.5 | Silverlight (Windows Phone 7) | |
![]() |
![]() |
Here is a comparison of capture an image of the current screen on Windows Phone 7 using Silverlight and .NET Compact Framework
.NET Compact Framework
private Bitmap CaptureScreen() { Bitmap b = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); using (Graphics g = Graphics.FromImage(b)) { IntPtr hdcSrc = IntPtr.Zero; IntPtr hdcDest = g.GetHdc(); try { //get the entire window by passing in IntPtr.Zero hdcSrc = GetWindowDC(IntPtr.Zero); //blit to the graphics object g BitBlt(hdcDest, 0, 0, b.Width, b.Height, hdcSrc, 0, 0, SRCCOPY); } finally { //Release any native src hdcs if (hdcSrc != IntPtr.Zero) { ReleaseDC(hdcSrc); DeleteDC(hdcSrc); } //Release the graphics hdc g.ReleaseHdc(hdcDest); } } return b; } [DllImportAttribute("coredll.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("coredll")] public static extern int DeleteDC(IntPtr hdc); [DllImportAttribute("coredll.dll")] internal static extern IntPtr ReleaseDC(IntPtr hdc); public const int SRCCOPY = 0x00CC0020; [DllImport("coredll.dll")] public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwROP);
Windows Phone 7 using Silverlight
private void btnCaptureScreen_Click(object sender, RoutedEventArgs e) { //Capture the screen and set it to the internal picture box WriteableBitmap bmp = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight); bmp.Render(this, null); bmp.Invalidate(); this.image1.Source = bmp; //Set a new background ImageBrush brush = new ImageBrush(); brush.ImageSource = new BitmapImage(new Uri(NextImage,UriKind.Relative)); ContentGrid.Background = brush; }
Essentially the apps do the same thing but it’s a lot easier to do it on one than the other. Here are some quick highlights on the differences.
- Silverlight for Windows Phone 7 has 7 lines of code while the .NET Compact Framework version has 28 lines of code
- With Silverlight for Windows Phone 7 you don’t have to deal with PInvokes anymore.
- Windows Phone 7 codebase is a lot smaller and more maintainable.
- With silverlight for Windows Phone 7, controls automatically support transparencies which was a challenge on NETCF 3.5 (notice the white box on NETCF 3.5 screen, that’s a PictureBox control
- Currently on Windows Phone 7 there is no way to get the images from your device in the form of a file unless you upload to a web service or something similar
Give it a try for yourself and download the sample code here.
Isn’t there a way to do a screen cap without code? I would hope there would be button sequence we can use (much like the iPhone)? Granted this cannot be done (easily) with the emulator, but when we finally get real devices, there should/has to be a way, no?
PHenry, hopefully there is a something similar to what the iPhone provides but at this point I don’t know since everything is still CTP. Code above provides an easy way to capture screen shots for your app for things like Marketplace for Mobile or a product page.