c#完成winform屏幕截圖並保留的示例。本站提示廣大學習愛好者:(c#完成winform屏幕截圖並保留的示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#完成winform屏幕截圖並保留的示例正文
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // 目的 DC的句柄
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc, // 源DC的句柄
int nXSrc,
int nYSrc,
System.Int32 dwRop // 光柵的處置數值
);
private void button1_Click(object sender, System.EventArgs e)
{
//取得以後屏幕的年夜小
Rectangle rect = new Rectangle ( ) ;
rect = Screen.GetWorkingArea ( this ) ;
//創立一個以以後屏幕為模板的圖像
Graphics g1 = this.CreateGraphics ( ) ;
//創立以屏幕年夜小為尺度的位圖
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 ( ) ;
//挪用此API函數,完成屏幕捕捉
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 ( "以後屏幕曾經保留為C盤的capture.jpg文件!" ) ;
}