程序对桌面使用Win32 API进行屏幕截图 [英] Program to take a screen shot of desktop usng Win32 API

查看:768
本文介绍了程序对桌面使用Win32 API进行屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

团队,
您能指导我一些概念/文章吗?它们可以帮助我编写一个程序来在用户工作或闲置时拍摄桌面屏幕快照/视频并将其保存到文件夹中.会发生这种情况吗?如果是这样,请帮助我.

问候,
Kiran

Hi Team,
Can you please guide me the concepts/articles which would help me to write a program to take a screen shot/ Video of the desktop while a user is working or idle and save it to a folder. Can this happen. if so please help me.

Regards,
Kiran

推荐答案



使用gdi32 api捕获屏幕截图非常容易.
请看James Crowley的这篇文章

http://www.developerfusion.com/code/4630/capture-a-screen-shot / [^ ]

Hi,

It''s fairly easy to capture screenshots using the gdi32 api.
Look at this article by James Crowley

http://www.developerfusion.com/code/4630/capture-a-screen-shot/[^]

public Image CaptureWindow(IntPtr handle)
{
    // get te hDC of the target window
    IntPtr hdcSrc = User32.GetWindowDC(handle);
    // get the size
    User32.RECT windowRect = new User32.RECT();
    User32.GetWindowRect(handle,ref windowRect);
    int width = windowRect.right - windowRect.left;
    int height = windowRect.bottom - windowRect.top;
    // create a device context we can copy to
    IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
    // create a bitmap we can copy it to,
    // using GetDeviceCaps to get the width/height
    IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
    // select the bitmap object
    IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
    // bitblt over
    GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
    // restore selection
    GDI32.SelectObject(hdcDest,hOld);
    // clean up
    GDI32.DeleteDC(hdcDest);
    User32.ReleaseDC(handle,hdcSrc);
    // get a .NET image object for it
    Image img = Image.FromHbitmap(hBitmap);
    // free up the Bitmap object
    GDI32.DeleteObject(hBitmap);
    return img;
}



如果要捕获视频,则可以查看Microsoft Expression编码器.

这是显示视频捕获的代码示例:



If you want to capture video then you could have a look Microsoft Expression encoder.

here is a code sample showing video capture:

_screenCaptureJob = new ScreenCaptureJob();
int width = Convert.ToInt32(area.Width / 4) * 4;
int height = Convert.ToInt32(area.Height / 4) * 4;
Rectangle rect = new Rectangle(area.Location, new Size(width, height));
_screenCaptureJob.CaptureRectangle = rect;
_screenCaptureJob.ShowFlashingBoundary = true;
_screenCaptureJob.ScreenCaptureVideoProfile.FrameRate = 8;
_screenCaptureJob.ScreenCaptureVideoProfile.Quality = 75;
_screenCaptureJob.CaptureMouseCursor = true;
_screenCaptureJob.OutputScreenCaptureFileName = Path.ChangeExtension(Path.Combine(this.textBox1.Text, Path.GetRandomFileName()), ".wmv");
_screenCaptureJob.Duration = new TimeSpan(0, 0, Convert.ToInt32(textBoxVideoLength.Text));
_screenCaptureJob.Start();



这两个代码示例在c#中.

瓦莱里.



These two code sample are in c#.

Valery.


这篇关于程序对桌面使用Win32 API进行屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆