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#...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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; } } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |