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 matterbi.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 bitsbfh.bfType = 0x4d42;
//BMbfh.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;- a method to get a screenshot of a WinCE 5.0 device
- How to Save a Screenshot
- How to Save a Screenshot
- XCode 8 Take a screenshot on a device
- Write a method to compute all permutations of a string
- How to get a handle to a device driver returned by device manager?
- Signature of a method
- How to Create a Device Driver --WINCE 6.0
- How to save a picture (take screenshot) from a camera
- How to get information of .a/.so
- How to GET a Cup of Coffee
- How to take screenshot (thumbnail) of a web site with ASP.NET 2.0?
- Processing a Request to Remove a Device
- how to change the default mode of a linux device
- How to Accept/Reject the Transfer of a Device
- Taking a screenshot of current Activity in Android
- a method to set the title of webpart in code
- Q9.5 Write a method to compute all permutations of a string
- ASP.NET是什么?
- 泡mm的经典语录.女的回答更是绝!
- 拼写检查——fly on spell
- 使用Game API开发J2ME 2D游戏
- “生物安全”概念引入到了密码系统中
- a method to get a screenshot of a WinCE 5.0 device
- RO10 – 不要频繁调用服务:通过最小调用改善执行
- 分析进程 识别病毒木马隐匿之术
- a different object with the same identifier value was already associated with the session的解决办法
- ASP.NET 服务器控件授权要求
- oracle-merge用法详解
- 认识病毒的映象劫持技术
- 在TMS320F2812上实现从flash拷贝整个程序到RAM上运行的方法探讨
- .Net 中的许可证机制