Somebody know a method to get a screenshot of a WinCE 5.0 device
hello,
check the following code
call method:
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 matterbi.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 bytebi.biSizeImage = (
uint)cb;bi.biXPelsPerMeter = XPelsPerMeter;
bi.biYPelsPerMeter = YPelsPerMeter;
IntPtr pBits = IntPtr.Zero;
//Allocate memory for bitmap bitsIntPtr pBI = LocalAlloc(GPTR, bi.biSize);
// Not sure if this needed - simply trying to keep marshaller happyMarshal.StructureToPtr(bi, pBI,
false); //This will return IntPtr to actual DIB bits in pBitsIntPtr 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 stuffIntPtr 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 dataMarshal.Copy(pBits, RealBits, 0, cb);
// This simply creates valid bitmap file header, so it can be saved to diskBITMAPFILEHEADER bfh =
new BITMAPFILEHEADER();bfh.bfSize = (
uint)cb + 0x36; // Size of header + size of BITMAPINFOHEADER size of bitmap bitsbfh.bfType = 0x4d42;
//BMbfh.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]; //BITMAPFILEHEADERheader.CopyTo(data, 0);
//BITMAPINFOHEADERheader =
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 bitsRealBits.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;}
}