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.
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.
Give it a try for yourself and download the sample code here.
Powered by: newtelligence dasBlog 1.9.7174.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
E-mail
Theme design by Jelle Druyts