vb.net中如何拷贝屏幕?

zxh168 2006-12-08 04:05:20
亦即实现 printscreen键的功能?


谢谢!
...全文
315 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicochang 2006-12-09
  • 打赏
  • 举报
回复
直接调用当然是不行了

Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
Graphics gs = Graphics.FromImage(bmp);
gs.CopyFromScreen(10, 10, 100, 100, new Size(90,90));
bmp.Save(@"c:\1.bmp");

类似这样调用
zxh168 2006-12-09
  • 打赏
  • 举报
回复
谢谢

我用vs2005 好象也没找到Graphics.CopyFromScreen

???

谢谢诸位!
zmaini1420 2006-12-09
  • 打赏
  • 举报
回复
不错帮顶一下
nicochang 2006-12-09
  • 打赏
  • 举报
回复
这段C#代码,如果,不好转成VB的话,可以用C#编译,单独成为一个DLL,用VB调用
nicochang 2006-12-09
  • 打赏
  • 举报
回复
对不起,不好意思,忘贴了一段API调用
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;

}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct TOKEN_PRIVILEGES
{
public uint PrivilegeCount;
public LUID Luid;
public uint Attributes;
}

public class Win32API
{
[DllImport("Advapi32", CharSet=CharSet.Auto)]
public static extern bool LookupPrivilegeValue (string sysname,string privname,ref LUID luid);

[DllImport("advapi32", CharSet=CharSet.Auto)]
public static extern bool AdjustTokenPrivileges(IntPtr handle, bool dsall,ref TOKEN_PRIVILEGES newstate,int len, IntPtr oldstate,IntPtr retlen);

[DllImport("kernel32.dll")] 
public static extern int GetLastError(); 

[DllImport("user32.dll")]
public static extern bool ExitWindowsEx(int uFlags, int dwReason); 
[DllImport("gdi32.dll")]
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
);

[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc // handle to DC
);

[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(
IntPtr hdc, // handle to DC
int nIndex // index of capability
);

[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, // handle to DC
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
);

[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, // handle to DC
IntPtr hgdiobj // handle to object
);

[DllImport("gdi32.dll")]
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
);

[DllImport("gdi32.dll")]
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
}

}
zxh168 2006-12-09
  • 打赏
  • 举报
回复
谢谢!

领教各位高招了!

十分感激,马上给分!
KahnWinsock 2006-12-08
  • 打赏
  • 举报
回复
To zxh168()

如果你用的是2005,.NET Framework 2.0 版中是新增了Graphics.CopyFromScreen 方法。
试试看。
KahnWinsock 2006-12-08
  • 打赏
  • 举报
回复
To zxh168()

2003 or 2005
zxh168 2006-12-08
  • 打赏
  • 举报
回复
VB里面怎么没有?


DX请再出招,谢谢!
tioncai 2006-12-08
  • 打赏
  • 举报
回复
很不错,收藏
顺便问一下。可以实现象QQ抓图软件的功能吗?
nicochang 2006-12-08
  • 打赏
  • 举报
回复
给一段C#写的,应该不难看懂
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);
}
zxh168 2006-12-08
  • 打赏
  • 举报
回复
希望问题不是太难,也希望DX能支招!

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧