I've wondered many times, how it would've been if there's a 'Prt scrn' button on the Windows Mobiles.
Well, as I understand now, its the job of the developers around, to do it, programmatically, and give it to the masses.
Thanks to Microsoft, atleast they've provided the functions for doing this programmatically.
Here's the code to do it.
It's done by Platform Invoking some native methods
// Get the handle of the form's device context and create compatible
// graphics and bitmap objects
//
System.IntPtr srcDC = GetDC(IntPtr.Zero) // or pass the handle to the form.
System.Drawing.Bitmap bm = new System.Drawing.Bitmap (this.Width, this.Height);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage (bm);
// Get the handle to the graphics object's device context.
//
System.IntPtr bmDC = graphics.GetHdc ();
// Copy the form to the bitmap
//
BitBlt (bmDC, 0, 0, bm.Width, bm.Height, srcDC, 0, 0, 0x00CC0020 /*SRCCOPY*/);
// Release native resources
//
ReleaseDC(IntPtr.Zero,srcDC);
graphics.ReleaseHdc (bmDC);
graphics.Dispose ();
// Save the bitmap in jpeg format or whatever required
//
bool written = false;
int index = 0;
while (!written)
{
string filename = String.Format ("Snap{0}.jpg", index++);
if (!System.IO.File.Exists (filename))
{
try
{
bm.Save (filename, System.Drawing.Imaging.ImageFormat.Jpeg);
written = true;
}
catch (Exception x)
{
System.Diagnostics.Debug.WriteLine (x);
}
}
That's it and you have the screen shot.
Modify it as necessary, such as to have the user choose the file name, save location, format, etc.
If you want to upload directly without saving it, create a Stream from the Bitmap, and you can upload it to your server.
If you want to just display the image, create an Image from the Bitmap and set it to the PictureBox.
That's it. Pretty simple.
No comments:
Post a Comment