Screen Capture in the Compact Framework

So I was reading the Windows Mobile Team Blog and to my surprise I saw “How to capture a screen shot with .NET CF” as a topic that actually made it to the top of their “upcoming articles” list.  Seriously?  I know the SDF provides that.  So I did some basic Googling and sure enough, unless you know what you’re looking for, it’s tough to find (but it’s been out for over a year).  For the record, the code is way too simple for a full article.  Even with the P/Invokes directly it’s pretty basic, but with the SDF, it looks like this (pulled from a sample web page for our Padarn web server):


// create a bitmap and graphics objects for the capture
Drawing.Bitmap destinationBmp = new Drawing.Bitmap(Forms.Screen.PrimaryScreen.Bounds.Width, Forms.Screen.PrimaryScreen.Bounds.Height);
Drawing.Graphics g = Drawing.Graphics.FromImage(destinationBmp);
GraphicsEx gx = GraphicsEx.FromGraphics(g);

// capture the current screen
gx.CopyFromScreen(0, 0, 0, 0, Forms.Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

// save the file
destinationBmp.Save(filename, System.Drawing.Imaging.ImageFormat.Png);

// clean house
gx.Dispose();
g.Dispose();
destinationBmp.Dispose();


Simple enough, right?

One Comment

Leave a Reply