IT Community - Software Programming, Web Development and Technical Support

How can we capture the todays screen image programmatically in windows mobile 5.0 poc

This is a discussion on How can we capture the todays screen image programmatically in windows mobile 5.0 poc within the C# Programming forums, part of the Software Development category; How can we capture the todays screen image programmatically in windows mobile 5.0 pocket pc using C#...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-21-2007, 12:02 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Question How can we capture the todays screen image programmatically in windows mobile 5.0 poc

How can we capture the todays screen image programmatically in windows mobile 5.0 pocket pc using C#
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-21-2007, 01:46 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: How can we capture the todays screen image programmatically in windows mobile 5.0

BitmapUtils.Snapshot(null, @"\test.bmp");

Class to call from
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace Snapshot
{

public class BitmapUtils
{
const int XPelsPerMeter = 0xb12; // 72 ppi, 96 would work well too
const int YPelsPerMeter = 0xb12;

public static void Snapshot(Control ctl, string FileName)
{
IntPtr hDC;
if ( ctl != null )
{
ctl.Focus();
IntPtr hReal = GetFocus();
hDC = GetDC(hReal);
}
else
{
hDC = GetDC(IntPtr.Zero); //Full screen
}
IntPtr hMemDC = CreateCompatibleDC(hDC);
BITMAPINFOHEADER bi = new BITMAPINFOHEADER();
bi.biSize = (uint)Marshal.SizeOf(bi);
bi.biBitCount = 24; // Creating RGB bitmap. The following three members don't matter
bi.biClrUsed = 0;
bi.biClrImportant = 0;
bi.biCompression = 0;
bi.biHeight = ctl != null? ctl.Height: Screen.PrimaryScreen.Bounds.Height;
bi.biWidth = ctl != null? ctl.Width: Screen.PrimaryScreen.Bounds.Width;
bi.biPlanes = 1;
int cb = (int)(bi.biHeight * bi.biWidth * bi.biBitCount / 8); //8 is bits per byte
bi.biSizeImage = (uint)cb;
bi.biXPelsPerMeter = XPelsPerMeter;
bi.biYPelsPerMeter = YPelsPerMeter;
IntPtr pBits = IntPtr.Zero;
//Allocate memory for bitmap bits
IntPtr pBI = LocalAlloc(GPTR, bi.biSize);
// Not sure if this needed - simply trying to keep marshaller happy
Marshal.StructureToPtr(bi, pBI, false);
//This will return IntPtr to actual DIB bits in pBits
IntPtr hBmp = CreateDIBSection(hDC, pBI, 0, ref pBits, IntPtr.Zero, 0);
//Marshall back - now we have BITMAPINFOHEADER correctly filled in
//Marshal.PtrToStructure(pBI, bi);
BITMAPINFOHEADER biNew = (BITMAPINFOHEADER)Marshal.PtrToStructure(pBI, typeof( BITMAPINFOHEADER ));
//Usual stuff
IntPtr hOldBitmap = SelectObject(hMemDC, hBmp);
//Grab bitmap
int nRet = BitBlt(hMemDC, 0, 0, bi.biWidth, bi.biHeight, hDC, 0, 0, SRCCOPY);
// Allocate memory for a copy of bitmap bits
byte[] RealBits = new byte[cb];
// And grab bits from DIBSestion data
Marshal.Copy(pBits, RealBits, 0, cb);
// This simply creates valid bitmap file header, so it can be saved to disk
BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
bfh.bfSize = (uint)cb + 0x36; // Size of header + size of BITMAPINFOHEADER size of bitmap bits
bfh.bfType = 0x4d42; //BM
bfh.bfOffBits = 0x36; //
int HdrSize = 14;
byte[] header = new byte[HdrSize];
BitConverter.GetBytes(bfh.bfType).CopyTo(header, 0);
BitConverter.GetBytes(bfh.bfSize).CopyTo(header, 2);
BitConverter.GetBytes(bfh.bfOffBits).CopyTo(header , 10);
//Allocate enough memory for complete bitmap file
byte[] data = new byte[cb + bfh.bfOffBits];
//BITMAPFILEHEADER
header.CopyTo(data, 0);
//BITMAPINFOHEADER
header = new byte[Marshal.SizeOf(bi)];
IntPtr pHeader = LocalAlloc(GPTR, (uint)Marshal.SizeOf(bi));
Marshal.StructureToPtr(biNew, pHeader, false);
Marshal.Copy(pHeader, header, 0, Marshal.SizeOf(bi));
LocalFree(pHeader);
header.CopyTo(data, HdrSize);

//Bitmap bits
RealBits.CopyTo(data, (int)bfh.bfOffBits);
FileStream fs = new FileStream(FileName, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
data = null;

DeleteObject(SelectObject(hMemDC, hOldBitmap));
DeleteDC(hMemDC);
ReleaseDC(hDC);
}
struct BITMAP
{
public int bmType;
public int bmWidth;
public int bmHeight;
public int bmWidthBytes;
public ushort bmPlanes;
public ushort bmBitsPixel;
public byte[] bmBits;
}

struct BITMAPINFO
{
public BITMAPINFOHEADER bmiHeader;
public RGBQUAD[] bmiColors;
}
struct RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}
struct BITMAPINFOHEADER
{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
}

struct BITMAPFILEHEADER
{
public ushort bfType;
public uint bfSize;
public ushort bfReserved1;
public ushort bfReserved2;
public uint bfOffBits;
}
const int GPTR = 0x40;
[DllImport("coredll.dll")]
internal static extern IntPtr SHLoadDIBitmap(
string szFileName );
[DllImport("aygshell.dll", EntryPoint="#75")]
internal static extern IntPtr SHLoadImageFile(
string szFileName );
[DllImport("coredll.dll")]
private static extern IntPtr GetFocus();
[DllImport("coredll.dll")]
private static extern IntPtr LocalAlloc(uint flags, uint cb);
[DllImport("coredll.dll")]
private static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("coredll.dll")]
private static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFOHEADER hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);
[DllImport("coredll.dll")]
private static extern IntPtr CreateDIBSection(IntPtr hdc, IntPtr hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);
[DllImport("coredll.dll")]
private static extern IntPtr GetStockObject(int obj);
[DllImport("coredll.dll")]
private static extern int GetObject(IntPtr hObj, int cb, [In, Out]byte[] pb);
[DllImport("coredll.dll")]
private static extern int GetObject(IntPtr hObj, int cb, [In, Out]int[] pb);
[DllImport("coredll.dll")]
private static extern int GetObject(IntPtr hObj, int cb, IntPtr obj);
[DllImport("coredll.dll")]
private static extern int GetObject(IntPtr hObj, int cb, ref BITMAP obj);
[DllImport ("coredll.dll")]
public static extern IntPtr GetDC( IntPtr hWnd );
[DllImport ("coredll.dll")]
public static extern void ReleaseDC( IntPtr hDC );
[DllImport ("coredll.dll")]
public static extern void DeleteDC( IntPtr hDC );
[DllImport ("coredll.dll")]
public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
[DllImport ("coredll.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport ("coredll.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport ("coredll.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObj);
[DllImport ("coredll.dll")]
public static extern void DeleteObject( IntPtr hObj );

const int SRCCOPY = 0x00CC0020;
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we move an image programmatically in pocket pc without flickering using C#? mobilegeek Mobile Software Development 3 04-23-2008 09:17 PM
How to animate an image using C# windows application programmatically? mobilegeek C# Programming 1 07-30-2007 12:42 AM
How to change the home screen programmatically Windows Mobile? theone Windows Mobile 1 07-27-2007 06:22 AM
Is there a way to Lock a Windows Mobile Device Programmatically so that no user can u itbarota Windows Mobile 0 07-23-2007 11:10 PM
Can we capture the desktop background image programatically using c#? mobilegeek C# Programming 1 07-20-2007 01:10 AM


All times are GMT -7. The time now is 08:40 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0