using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace PrintScreen
{
//一部分代碼來自網絡
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
//封裝一部分api的類
public class apitmp
{
[DllImport("user32.dll")]
public static extern bool GetCursorPos(ref Point lpPoint);
[DllImport("user32.dll")]
public static extern int WindowFromPoint( Point lpPoint);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern int GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowRect(int hwnd, ref RECT rc);
[DllImport("user32.dll")]
public static extern int GetWindowDC( int hwnd);
[DllImport("Gdi32.dll")]
public static extern bool BitBlt (
IntPtr hdcDest , // 目标裝置的句柄
int nXDest , // 目标對象的左上角的X坐标
int nYDest , // 目标對象的左上角的X坐标
int nWidth , // 目标對象的矩形的寬度
int nHeight , // 目标對象的矩形的長度
IntPtr hdcSrc , // 源裝置的句柄
int nXSrc , // 源對象的左上角的X坐标
int nYSrc , // 源對象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
) ;
[DllImportAttribute ( "gdi32.dll" ) ]
public static extern IntPtr CreateDC (
string lpszDriver , // 驅動名稱
string lpszDevice , // 裝置名稱
string lpszOutput , // 無用,可以設定位"NULL"
IntPtr lpInitData // 任意的列印機資料
) ;
}
//封裝各種抓屏操作的類
public class UCapture
{
//獲得螢幕指定區域
public Bitmap getscreen(int left,int top ,int width,int height)
{
IntPtr dc1 = apitmp.CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
Graphics newGraphics = Graphics.FromHdc(dc1);
Bitmap img = new Bitmap(width , height ,newGraphics);
Graphics thisGraphics = Graphics.FromImage(img);
IntPtr dc2 =thisGraphics.GetHdc();
IntPtr dc3 = newGraphics.GetHdc();
apitmp.BitBlt(dc2,0,0,width , height ,dc3,left,top,13369376);
thisGraphics.ReleaseHdc(dc2);
newGraphics.ReleaseHdc(dc3);
return img;
}
//獲得整個螢幕
public Bitmap getfullscreen()
{
return getscreen(0,0,Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
}
//抓取句柄所指視窗
public Bitmap getscreenfromhandle(int hwnd)
{
RECT rc = new RECT();
apitmp.GetWindowRect(hwnd, ref rc);
return getscreen(rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top);
}
//抓取活動視窗
public Bitmap getscreenfromactivewindow()
{
int handle = apitmp.GetForegroundWindow();
return getscreenfromhandle(handle);
}
//儲存成各種格式
public string savepic(Bitmap bmp)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "位圖檔案 (*.bmp)|*.bmp|jpg檔案 (*.jpg)|*.jpg|gif檔案 (*.gif)|*.gif|tiff檔案 (*.tiff)|*.tiff|"+
"emf檔案 (*.emf)|*.emf|圖示檔案 (*.ico)|*.ico|wmf檔案 (*.wmf)|*.wmf|png檔案 (*.png)|*.png";
saveDialog.DefaultExt="*.bmp";
if(saveDialog.ShowDialog() == DialogResult.OK)
{
string ext = saveDialog.FileName.Substring(saveDialog.FileName.Length-4,4);
switch( ext)
{
case ".bmp":
bmp.Save(saveDialog.FileName,ImageFormat.Bmp);
break;
case ".gif":
bmp.Save(saveDialog.FileName,ImageFormat.Gif);
break;
case ".jpg":
bmp.Save(saveDialog.FileName,ImageFormat.Jpeg);
break;
case ".emf":
bmp.Save(saveDialog.FileName,ImageFormat.Emf);
break;
case ".ico":
bmp.Save(saveDialog.FileName,ImageFormat.Icon);
break;
case ".wmf":
bmp.Save(saveDialog.FileName,ImageFormat.Wmf);
break;
case ".png":
bmp.Save(saveDialog.FileName,ImageFormat.Png);
break;
case ".tiff":
bmp.Save(saveDialog.FileName,ImageFormat.Tiff);
break;
default :
return "";
}
return saveDialog.FileName;
}
return "";
}
}
}