kevin_2009 发表于 2008-12-23 09:23:01

VB拷屏

我要用VB写个拷屏的程序,就是将屏幕拷屏,然后存入一个文件,整了好久没搞定,哪位兄弟可以帮忙啊?谢谢了!

ricken 发表于 2008-12-23 09:25:51

记得以前在一本书中看到过类似的文章,是远程拷屏的,有空再找一下吧.看看能不能找到.

ricken 发表于 2008-12-23 09:31:45

如果你用的是2005,.NET   Framework   2.0   版中是新增了Graphics.CopyFromScreen   方法。   
试试看。

ricken 发表于 2008-12-23 09:33:26

再有就是调用API的方式,以下内容是从网上找到的,可以参考一下,是用C#写的.
struct   GetDeviceCapsIndex   
{   
public   static   readonly   int   DRIVERVERSION   =   0;   
public   static   readonly   int   HORZSIZE   =   4;   
public   static   readonly   int   VERTSIZE   =   6;   
public   static   readonly   int   HORZRES   =   8;   
public   static   readonly   int   VERTRES   =   10;   
   
}   
   
   
public   struct   LUID   
{   
public   uint   LowPart;   
public   uint   HighPart;   
}   
   
   
public   struct   TOKEN_PRIVILEGES   
{   
public   uint   PrivilegeCount;   
public   LUID   Luid;   
public   uint   Attributes;   
}   
   
public   class   Win32API   
{   
   
public   static   extern   bool   LookupPrivilegeValue   (string   sysname,string   privname,ref   LUID   luid);   
   
   
public   static   extern   bool   AdjustTokenPrivileges(IntPtr   handle,   bool   dsall,ref   TOKEN_PRIVILEGES   newstate,int   len,   IntPtr   oldstate,IntPtr   retlen);   
   
    
public   static   extern   int   GetLastError();    
   
   
public   static   extern   bool   ExitWindowsEx(int   uFlags,   int   dwReason);    
   
public   static   extern   IntPtr   CreateDC(   
string   lpszDriver,               //   driver   name   
string   lpszDevice,               //   device   name   
string   lpszOutput,               //   not   used;   should   be   NULL   
Int64   lpInitData   //   optional   printer   data   
);   
      
   
public   static   extern   IntPtr   CreateCompatibleDC(   
IntPtr   hdc   //   handle   to   DC   
);   
      
   
public   static   extern   int   GetDeviceCaps(   
IntPtr   hdc,         //   handle   to   DC   
int   nIndex       //   index   of   capability   
);   
   
   
public   static   extern   IntPtr   CreateCompatibleBitmap(   
IntPtr   hdc,               //   handle   to   DC   
int   nWidth,         //   width   of   bitmap,   in   pixels   
int   nHeight         //   height   of   bitmap,   in   pixels   
);   
   
   
public   static   extern   IntPtr   SelectObject(   
IntPtr   hdc,                     //   handle   to   DC   
IntPtr   hgdiobj       //   handle   to   object   
);   
   
   
public   static   extern   int   BitBlt(   
IntPtr   hdcDest,   //   handle   to   destination   DC   
int   nXDest,   //   x-coord   of   destination   upper-left   corner   
int   nYDest,   //   y-coord   of   destination   upper-left   corner   
int   nWidth,   //   width   of   destination   rectangle   
int   nHeight,   //   height   of   destination   rectangle   
IntPtr   hdcSrc,   //   handle   to   source   DC   
int   nXSrc,       //   x-coordinate   of   source   upper-left   corner   
int   nYSrc,       //   y-coordinate   of   source   upper-left   corner   
UInt32   dwRop   //   raster   operation   code   
);   
   
   
public   static   extern   int   DeleteDC(   
IntPtr   hdc                     //   handle   to   DC   
);   
   
}   
public   class   GDI   
{   
public   static   Bitmap   GetScreen()   
{   
IntPtr   hscrdc,hmemdc;   
IntPtr   hbitmap,holdbitmap;   
int   nx,ny,nx2,ny2;   
nx=ny=nx2=ny2=0;   
int   nwidth,   nheight;   
int   xscrn,   yscrn;   
hscrdc   =   Win32API.CreateDC("DISPLAY",   null,   null,   0);//创建DC句柄   
hmemdc   =   Win32API.CreateCompatibleDC(hscrdc);//创建一个内存DC   
xscrn   =   Win32API.GetDeviceCaps(hscrdc,   GetDeviceCapsIndex.HORZRES);//获取屏幕宽度   
yscrn   =   Win32API.GetDeviceCaps(hscrdc,   GetDeviceCapsIndex.VERTRES);//获取屏幕高度   
nx   =   0;   
ny   =   0;   
nx2   =   xscrn;   
ny2   =   yscrn;   
   
nwidth   =   nx2   -   nx;//截取范围的宽度   
nheight   =   ny2   -   ny;//截取范围的高度   
hbitmap   =   Win32API.CreateCompatibleBitmap(hscrdc,   nwidth,   nheight);//从内存DC复制到hbitmap句柄   
holdbitmap   =   Win32API.SelectObject(hmemdc,   hbitmap);   
Win32API.BitBlt(hmemdc,   0,   0,   nwidth,   nheight,hscrdc,   nx,   ny,(UInt32)0xcc0020);   
hbitmap   =   Win32API.SelectObject(hmemdc,   holdbitmap);   
Win32API.DeleteDC(hscrdc);//删除用过的对象   
Win32API.DeleteDC(hmemdc);//删除用过的对象   
return   Bitmap.FromHbitmap(hbitmap);//用Bitmap.FromHbitmap从hbitmap返回Bitmap   
}   
   
}   

以上的调用的API声明,以下是主程序:
public   virtual   void   OnGetScreen()   
{   
Bitmap   bmp   =   GDI.GetScreen();   
Sender   sender   =   new   Sender();   
MemoryStream   mem   =   new   MemoryStream();   
bmp.Save(mem   ,   System.Drawing.Imaging.ImageFormat.Jpeg);   
bmp.Dispose();   
byte[]   pic;   
pic   =   mem.GetBuffer();   
mem.Close();   
sender.Send(pic);   
}

ricken 发表于 2008-12-23 09:37:04

以下是一个网友写的VB拷屏的例子,你可以参考一下.

kevin_2009 发表于 2008-12-26 09:03:24

谢谢兄弟们啊

kevin_2009 发表于 2008-12-26 19:07:05

Ricken:
    我还想问一下:如果要将指定的窗口拷屏下来(比方说,现在应用程序正在运行,如果要将计算器的界面拷下来),该怎么整呢?如何将拷屏的图像存入文件呢?谢谢
页: [1]
查看完整版本: VB拷屏