a method to get a screenshot of a WinCE 5.0 device

来源:互联网 发布:黑桐干也 知乎 编辑:程序博客网 时间:2024/06/06 12:53

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

{

 

 

{

 

Snapshotpublic class BitmapUtilsconst int XPelsPerMeter = 0xb12; // 72 ppi, 96 would work well too

 

 

 

{

IntPtr hDC;

 

{

ctl.Focus();

IntPtr hReal = GetFocus();

hDC = GetDC(hReal);

}

 

const int YPelsPerMeter = 0xb12; public static void Snapshot(Control ctl, string FileName)if ( ctl != null )else

{

hDC = GetDC(IntPtr.Zero);

//Full screen

}

IntPtr hMemDC = CreateCompatibleDC(hDC);

BITMAPINFOHEADER bi =

bi.biSize = (

bi.biBitCount = 24;

new BITMAPINFOHEADER();uint)Marshal.SizeOf(bi);// Creating RGB bitmap. The following three members don't matter

bi.biClrUsed = 0;

bi.biClrImportant = 0;

bi.biCompression = 0;

bi.biHeight = ctl !=

bi.biWidth = ctl !=

bi.biPlanes = 1;

 

null? ctl.Height: Screen.PrimaryScreen.Bounds.Height;null? ctl.Width: Screen.PrimaryScreen.Bounds.Width;int cb = (int)(bi.biHeight * bi.biWidth * bi.biBitCount / 8); //8 is bits per byte

bi.biSizeImage = (

bi.biXPelsPerMeter = XPelsPerMeter;

bi.biYPelsPerMeter = YPelsPerMeter;

IntPtr pBits = IntPtr.Zero;

 

uint)cb;//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 =

bfh.bfSize = (

new BITMAPFILEHEADER();uint)cb + 0x36; // Size of header + size of BITMAPINFOHEADER size of bitmap bits

bfh.bfType = 0x4d42;

//BM

bfh.bfOffBits = 0x36;

//

 

 

BitConverter.GetBytes(bfh.bfType).CopyTo(header, 0);

BitConverter.GetBytes(bfh.bfSize).CopyTo(header, 2);

BitConverter.GetBytes(bfh.bfOffBits).CopyTo(header, 10);

 

int HdrSize = 14;byte[] header = new byte[HdrSize];//Allocate enough memory for complete bitmap file

 

 

byte[] data = new byte[cb + bfh.bfOffBits];//BITMAPFILEHEADER

header.CopyTo(data, 0);

 

//BITMAPINFOHEADER

header =

IntPtr pHeader = LocalAlloc(GPTR, (

Marshal.StructureToPtr(biNew, pHeader,

Marshal.Copy(pHeader, header, 0, Marshal.SizeOf(bi));

LocalFree(pHeader);

header.CopyTo(data, HdrSize);

 

 

new byte[Marshal.SizeOf(bi)];uint)Marshal.SizeOf(bi));false);//Bitmap bits

RealBits.CopyTo(data, (

FileStream fs =

fs.Write(data, 0, data.Length);

fs.Flush();

fs.Close();

data =

 

DeleteObject(SelectObject(hMemDC, hOldBitmap));

DeleteDC(hMemDC);

ReleaseDC(hDC);

}

 

{

 

 

 

 

 

 

 

}

 

 

{

 

 

}

 

{

 

 

 

 

}

 

{

 

 

 

 

 

 

 

 

 

 

 

}

 

 

{

 

 

 

 

 

}

 

[DllImport("coredll.dll")]

 

 

[DllImport("aygshell.dll", EntryPoint="#75")]

 

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

[DllImport ("coredll.dll")]

 

 

 

}

}

int)bfh.bfOffBits);new FileStream(FileName, FileMode.Create);null;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 BITMAPINFOHEADERpublic 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;internal static extern IntPtr SHLoadDIBitmap(string szFileName ); internal static extern IntPtr SHLoadImageFile(string szFileName ); private static extern IntPtr GetFocus();private static extern IntPtr LocalAlloc(uint flags, uint cb);private static extern IntPtr LocalFree(IntPtr hMem);private static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFOHEADER hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);private static extern IntPtr CreateDIBSection(IntPtr hdc, IntPtr hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);private static extern IntPtr GetStockObject(int obj);private static extern int GetObject(IntPtr hObj, int cb, [In, Out]byte[] pb);private static extern int GetObject(IntPtr hObj, int cb, [In, Out]int[] pb);private static extern int GetObject(IntPtr hObj, int cb, IntPtr obj);private static extern int GetObject(IntPtr hObj, int cb, ref BITMAP obj);public static extern IntPtr GetDC( IntPtr hWnd );public static extern void ReleaseDC( IntPtr hDC );public static extern void DeleteDC( IntPtr hDC );public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); public static extern IntPtr CreateCompatibleDC(IntPtr hdc);public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObj);public static extern void DeleteObject( IntPtr hObj );const int SRCCOPY = 0x00CC0020; 
原创粉丝点击