我正在创build一个Windows应用程序使用c#。我有一个button,应该捕获图像(整个桌面屏幕),并将其保存在一个文件夹中。我还需要显示图像的预览。
Graphics.CopyFromScreen方法
示例代码:
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size); g.Save(); bmp.Save("D:\\file.jpg", ImageFormat.Bmp);
至于展示预览。 国际海事组织不是很难写你自己的。
有不同的方式来执行你在这里带来的。 使用Screen
类,我在互联网上找到了一些简单的例子。 其他人正在使用Direct3D。
总之,这个想法包括使用Screen
类或者您喜欢的方式获取桌面的图像,将其存储到Bitmap
对象中并将该位图保存到文件中。
至于显示预览,一旦你的Bitmap
实例被创建,你只需要一个PictureBox
并设置它的Image
属性,并向用户显示你的表单,以便他可以看到图像。
希望这可以帮助! =)
你将需要做一些Interop dll的导入。
看看下面的例子,非常好地显示如何捕获屏幕截图并保存到磁盘。
public void CaptureScreen(string fileName,ImageFormat imageFormat) { int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()), hdcDest = GDI32.CreateCompatibleDC(hdcSrc), hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, GDI32.GetDeviceCaps(hdcSrc,8),GDI32.GetDeviceCaps(hdcSrc,10)); GDI32.SelectObject(hdcDest,hBitmap); GDI32.BitBlt(hdcDest,0,0,GDI32.GetDeviceCaps(hdcSrc,8), GDI32.GetDeviceCaps(hdcSrc,10),hdcSrc,0,0,0x00CC0020); SaveImageAs(hBitmap,fileName,imageFormat); Cleanup(hBitmap,hdcSrc,hdcDest); }
上面的例子取自网站。 Perry Lee的所有代码