c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // 目標(biāo) DC的句柄
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc, // 源DC的句柄
int nXSrc,
int nYSrc,
System.Int32 dwRop // 光柵的處理數(shù)值
);
private void button1_Click(object sender, System.EventArgs e)
{
//獲得當(dāng)前屏幕的大小
Rectangle rect = new Rectangle ( ) ;
rect = Screen.GetWorkingArea ( this ) ;
//創(chuàng)建一個(gè)以當(dāng)前屏幕為模板的圖象
Graphics g1 = this.CreateGraphics ( ) ;
//創(chuàng)建以屏幕大小為標(biāo)準(zhǔn)的位圖
Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;
Graphics g2 = Graphics.FromImage ( MyImage ) ;
//得到屏幕的DC
IntPtr dc1 = g1.GetHdc ( ) ;
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc ( ) ;
//調(diào)用此API函數(shù),實(shí)現(xiàn)屏幕捕獲
BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
//釋放掉屏幕的DC
g1.ReleaseHdc ( dc1 ) ;
//釋放掉Bitmap的DC
g2.ReleaseHdc ( dc2 ) ;
//以JPG文件格式來保存
MyImage.Save ( @"c:/Capture.jpg" , ImageFormat.Jpeg );
MessageBox.Show ( "當(dāng)前屏幕已經(jīng)保存為C盤的capture.jpg文件!" ) ;
}
相關(guān)文章
C#實(shí)現(xiàn)多線程的同步方法實(shí)例分析
這篇文章主要介紹了C#實(shí)現(xiàn)多線程的同步方法,實(shí)例分析了C#線程同步的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
服務(wù)器端C#實(shí)現(xiàn)的CSS解析器
服務(wù)器端C#實(shí)現(xiàn)的CSS解析器2008-09-09

