惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - lmllouk

找nopcommerce兼职人员 ArachNode.Net 之配置 CS2001 CS2008 Adobe CS4 Dreamweaver 产品许可证已过期 CSS实现网页背景渐变 jQuery 多级下拉菜单解决方案 确保已安装类型(.aspx)的应用程序 sql server 行合并 灌水 的论坛 取消svn版本控制 - lmllouk - 博客园 fatal error C1083: Cannot open include file: 'ceconfig.h': No such file or directory PowerDesigner constraint name 长度限制问题 不同服务器数据库之间的数据操作 使用PowerDesigner进行代码生成(转) 怎样给 ActiveX 控件签名并打包发布 温州到杭州火车 通过CertEnroll在CA上(1创建证书请求2得到证书3安装证书) orcale 常见错误 C++ BUILDER AnsiString 用法 - lmllouk
抓屏 - lmllouk - 博客园
lmllouk · 2010-04-28 · via 博客园 - lmllouk

[DllImport("CoreDll.dll", EntryPoint = "GetDesktopWindow")]
  public static extern IntPtr GetDesktopWindow();

  [DllImport("CoreDll.dll", EntryPoint = "GetSystemMetrics")]
  public static extern int GetSystemMetrics(int abc);

  [DllImport("CoreDll.dll", EntryPoint = "GetWindowDC")]
  public static extern IntPtr GetWindowDC(IntPtr ptr);

  [DllImport("coredll.dll")]
  internal static extern IntPtr GetDC(IntPtr hWnd);

  [DllImport("coredll.dll")]
  internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

  [DllImport("coredll.dll")]
  internal static extern bool BringWindowToTop(IntPtr hWnd);

  [DllImport("coredll", EntryPoint = "ExtEscape")]
  private static extern int ExtEscape(
  IntPtr hdc,
  uint nEscape,
  uint cbInput,
  byte[] lpszInData,
  int cbOutput,
  IntPtr lpszOutData
  );
  [DllImport("CoreDLL.dll")]
  public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  [DllImport("CoreDLL.dll")]
  public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
  int nWidth, int nHeight, IntPtr hObjectSource,
  int nXSrc, int nYSrc, int dwRop);

  // Window State and Position Functions.

  [DllImport("CoreDLL.dll")]
  private static extern IntPtr GetCapture();

  [DllImport("CoreDLL.dll")]
  public static extern IntPtr SetFocus(IntPtr hWnd);

  [DllImport("CoreDLL.dll")]
  private static extern bool SetWindowPos(IntPtr hWnd,
  IntPtr hWndInsertAfter,
  int X,
  int Y,
  int cx,
  int cy,
  int uFlags);

  [DllImport("CoreDLL.dll")]
  public static extern bool DeleteDC(IntPtr hDC);
  [DllImport("CoreDLL.dll")]
  public static extern bool DeleteObject(IntPtr hObject);

  [DllImport("CoreDLL.dll", EntryPoint = "SetForegroundWindow")]
  private static extern bool SetForegroundWindow(System.IntPtr hWnd);

  [DllImport("coredll.dll", EntryPoint = "GetForegroundWindow", SetLastError = true)]
  public static extern IntPtr GetForegroundWindow();

  [DllImport("CoreDLL.dll")]
  public static extern IntPtr FindWindow(string className, string WindowsName);

把上面的内容包装到Display类中,然后。

  public static Bitmap SnapShot()
  {
  Bitmap bmp = new Bitmap(240,320);
  IntPtr hwnd = Display.GetDesktopWindow();
  int width = Display.GetSystemMetrics(Display.SM_CXSCREEN);
  int height = Display.GetSystemMetrics(Display.SM_CYSCREEN);

  IntPtr hdcsrc = Display.GetWindowDC(hwnd);
  IntPtr hdcdes = Display.CreateCompatibleDC(hdcsrc);
  IntPtr hbitmap = Display.CreateCompatibleBitmap(hdcsrc, width, height);
  IntPtr hOld = Display.SelectObject(hdcdes, hbitmap);

  Display.BitBlt(hdcdes, 0, 0, width, height, hdcsrc, 0, -26, Display.SRCCOPY);
  Display.SelectObject(hdcdes, hOld);
  try
  {
  bmp = Bitmap.FromHbitmap(hbitmap);
  Display.DeleteObject(hbitmap);
  Display.DeleteObject(hOld);
  Display.DeleteDC(hdcdes);
  Display.DeleteDC(hdcsrc);
  Display.ReleaseDC(hwnd, hdcsrc);
  return bmp;
  }
  catch (OutOfMemoryException ome)
  {
  Debug.Write("MEM ERROR:"+ome.ToString());
  GC.Collect();
  }
  return bmp;

  }
  }

void ScreenCapture::SaveScreenToBmpFile(const char *filename)

HDC hScrDC, hMemDC;  
int width, height,pixel_cnt; 

//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL; 
//creates a device context for the screen device 
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL); 

//get the screen point size
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES); 
pixel_cnt = 3 * width * height;

//creates a memory device context (DC) compatible with the screen device(hScrDC)  
hMemDC = CreateCompatibleDC(hScrDC); 

//initialise the struct BITMAPINFO for the bimap infomation,
//in order to use the function CreateDIBSection
//on wince os, each pixel stored by 24 bits(biBitCount=24) 
//and no compressing(biCompression=0)
BITMAPINFO RGB24BitsBITMAPINFO; 
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;

//use the function CreateDIBSection and SelectObject 
//in order to get the bimap pointer : lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO, 
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp); 

// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);

//if you only want to get the every pixel color value, 
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file; 
//bimap file header in order to write bmp file
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4D42; //bmp  
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + (pixel_cnt); ///3=(24 / 8)

//if the screen does not have any change from last one, just return without saving the Bmp data.
if(! isScreenChanged(lpBitmapBits))
{
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(directBmp);
DeleteObject(previousObject);
return;
}

//write into file 
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))
{  
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);

//write bitmap info 
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);

//write bitmap pixels data
fwrite(lpBitmapBits, pixel_cnt, 1, mStream);
//close file
fclose(mStream);
}

//delete
DeleteObject(hMemDC);
DeleteObject(hScrDC);
//DeleteObject(directBmp);
DeleteObject(previousObject);
return;
}