private void simpleButton1_Click_1(object sender, EventArgs e)
{
ScreenShuts(0,0,800,600,100,300);
}
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
//截图保存图片
private void ScreenShuts(int intX, int intY, int intWidth, int intHeight, int intLeft, int intRight)
{
////获得当前窗体的大小
Rectangle rect = new Rectangle();
rect = this.Bounds;
//创建一个以当前窗体为模板的图象
Graphics g1 = this.CreateGraphics();
//创建以窗体大小为标准的位图
Bitmap MyBMP = new Bitmap(intWidth, intHeight, g1);//定义位图的大小
//创建一个位图Bitmap绘图图面
Graphics g2 = Graphics.FromImage(MyBMP);
//得到窗体的DC(句柄)
IntPtr dc1 = g1.GetHdc();
HandleRef hDcSrc = new HandleRef(null, dc1);
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc();
HandleRef hDcSave = new HandleRef(null, dc2);
//复制图块的光栅操作码
const int SRCCOPY = 0xcc0020;
//调用此API函数,实现窗体捕获
//BitBlt(dc2, intX, intY, intWidth, intHeight, dc1, intLeft, intRight, SRCCOPY);//13369376
BitBlt(hDcSave, intX, intY, intWidth, intHeight, hDcSrc, intLeft, intRight, SRCCOPY);//13369376
//释放掉屏幕的DC
g1.ReleaseHdc(dc1);
//释放掉Bitmap的DC
g2.ReleaseHdc(dc2);
//以JPG文件格式来保存
MyBMP.Save("c:\\1.bmp", ImageFormat.Bmp);//Application.StartupPath + "\\
}
{
ScreenShuts(0,0,800,600,100,300);
}
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
//截图保存图片
private void ScreenShuts(int intX, int intY, int intWidth, int intHeight, int intLeft, int intRight)
{
////获得当前窗体的大小
Rectangle rect = new Rectangle();
rect = this.Bounds;
//创建一个以当前窗体为模板的图象
Graphics g1 = this.CreateGraphics();
//创建以窗体大小为标准的位图
Bitmap MyBMP = new Bitmap(intWidth, intHeight, g1);//定义位图的大小
//创建一个位图Bitmap绘图图面
Graphics g2 = Graphics.FromImage(MyBMP);
//得到窗体的DC(句柄)
IntPtr dc1 = g1.GetHdc();
HandleRef hDcSrc = new HandleRef(null, dc1);
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc();
HandleRef hDcSave = new HandleRef(null, dc2);
//复制图块的光栅操作码
const int SRCCOPY = 0xcc0020;
//调用此API函数,实现窗体捕获
//BitBlt(dc2, intX, intY, intWidth, intHeight, dc1, intLeft, intRight, SRCCOPY);//13369376
BitBlt(hDcSave, intX, intY, intWidth, intHeight, hDcSrc, intLeft, intRight, SRCCOPY);//13369376
//释放掉屏幕的DC
g1.ReleaseHdc(dc1);
//释放掉Bitmap的DC
g2.ReleaseHdc(dc2);
//以JPG文件格式来保存
MyBMP.Save("c:\\1.bmp", ImageFormat.Bmp);//Application.StartupPath + "\\
}
*************************************万恶的分隔符.另一种******
private void smpBtnFind_Click(object sender, EventArgs e)
{
CaptureDesktop("c:\\1.bmp", this.Width, this.Height);
{
CaptureDesktop("c:\\1.bmp", this.Width, this.Height);
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(string driver, string device, IntPtr res1, IntPtr res2);
public enum TernaryRasterOperations
{
SRCCOPY = 0x00CC0020, /* dest = source*/
SRCPAINT = 0x00EE0086, /* dest = source OR dest*/
SRCAND = 0x008800C6, /* dest = source AND dest*/
SRCINVERT = 0x00660046, /* dest = source XOR dest*/
SRCERASE = 0x00440328, /* dest = source AND (NOT dest )*/
NOTSRCCOPY = 0x00330008, /* dest = (NOT source)*/
NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)*/
MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest*/
PATCOPY = 0x00F00021, /* dest = pattern*/
PATPAINT = 0x00FB0A09, /* dest = DPSnoo*/
PATINVERT = 0x005A0049, /* dest = pattern XOR dest*/
DSTINVERT = 0x00550009, /* dest = (NOT dest)*/
BLACKNESS = 0x00000042, /* dest = BLACK*/
WHITENESS = 0x00FF0062, /* dest = WHITE*/
}
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
public static void CaptureDesktop(string sPath, int Width, int Height)
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
//rect = Bounds;
FrmVideo ff = new FrmVideo();
rect.Width = Width;//ff.Width;//.Screen.PrimaryScreen.Bounds.Width;
rect.Height = Height;// Screen.PrimaryScreen.Bounds.Height;
IntPtr dcTmp = CreateDC("DISPLAY", "DISPLAY", (IntPtr)null, (IntPtr)null);
Graphics gScreen = Graphics.FromHdc(dcTmp);
Bitmap image = new Bitmap((int)(rect.Width), (int)(rect.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics gImage = Graphics.FromImage(image);
IntPtr dcImage = gImage.GetHdc();
IntPtr dcScreen = gScreen.GetHdc();
BitBlt(dcImage, 0, 0, (int)(rect.Width), (int)(rect.Height), dcScreen, (int)(rect.Left), (int)(rect.Top), TernaryRasterOperations.SRCCOPY);
gScreen.ReleaseHdc(dcScreen);
gImage.ReleaseHdc(dcImage);
image.Save(sPath);
}