尝试基于计时器的屏幕截图 [英] trying for screen shots based on timer
本文介绍了尝试基于计时器的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试基于计时器的屏幕截图如何实现请告诉我,我尝试了一些其他方法。就像我试图将屏幕截图保存到我得到它的文件夹
当我试图拍摄屏幕截图时我没有得到请帮帮我。
trying for screen shots based on the timer how to implement please tell me i tried some other methods . like i tried to save the screen shots to the folder i got it
when i am trying for to take screen shots i am not getting please help me out.
推荐答案
使用System;
使用System.Collections.Generic;
使用System.Text;
使用System.Runtime .InteropServices;
使用System.Drawing;
使用System.Drawing.Imaging;
namespace Hookclass < br $>
{
公共类ScreenCapture
{
public Image CaptureScreen()
{
返回CaptureWindow(User32.GetDesktopWindow());
}
public Image CaptureWindow(IntPtr句柄)
{
//获取目标窗口的hDC
IntPtr hdcSrc = User32.GetWindowDC(句柄);
//获取大小
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle,ref windowRect);
int width = windowRect.right - windowRect。 left;
int height = windowRect.bottom - windowRect.top;
//创建我们可以复制到的设备上下文
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
//创建一个我们可以复制到的位图,
//使用GetDeviceCaps获取宽度/高度
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
//选择位图对象
IntPtr hOld = GDI32 .SelectObject(hdcDest,hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32。 SRCCOPY);
//恢复选择
GDI32.SelectObject(hdcDest ,hOld);
//清理
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle,hdcSrc);
//为它获取一个.NET图像对象
图像img = Image.FromHbitmap(hBitmap);
//释放Bitmap对象
GDI32.DeleteObject(hBitmap);
返回img;
}
public void CaptureWindowToFile(IntPtr handle,string filename,ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(文件名,格式);
}
public void CaptureScreenToFile( string filename,ImageFormat格式)
{
Image img = CaptureScreen();
img.Save(filename,format);
}
私人班级GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop参数
[DllImport(gdi32.dll)]
public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hObjectSource,
[DllImport(gdi32.dll)]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
int nHeight);
[DllImport(gdi32.dll)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport(gdi32.dll)]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport(gdi32.dll)]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport( gdi32.dll)]
public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
}
私人班级用户32
{
[StructLayout (LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport(user32.dll) ]
public static extern IntPtr GetDesktopWindow();
[DllImport(user32.dll)]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport(user32.dll)]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
[DllImport(user32.dll)]
public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace Hookclass
{
public class ScreenCapture
{
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
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;
}
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename, format);
}
private class GDI32
{
public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
}
}
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);
bitmap.Save(@"c:\screenshot1.bmp", ImageFormat.Bmp);
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
这篇关于尝试基于计时器的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文