Screen screen = Screen.PrimaryScreen;
int ScreenWith = screen.Bounds.Width;
int ScreenHeight = screen.Bounds.Height;
Graphics g = this.CreateGraphics();
//创建一个以当前屏幕为模板的图象
Image myimage = new Bitmap(ScreenWith, ScreenHeight, g);
//创建以屏幕大小为标准的位图
Graphics gg = Graphics.FromImage(myimage);
IntPtr dc = g.GetHdc();//得到屏幕的DC
IntPtr dcdc = gg.GetHdc();//得到Bitmap的DC
BitBlt(dcdc, 0, 0, ScreenWith, ScreenHeight, dc, 0, 0, 13369376);
//调用此API函数,实现屏幕捕获
g.ReleaseHdc(dc);//释放掉屏幕的DC
gg.ReleaseHdc(dcdc);//释放掉Bitmap的DC
myimage.Save(Application.StartupPath + @"\截屏.jpg", ImageFormat.Jpeg);
this.Show();
MessageBox.Show("截屏成功!");
this.Close();
得到的为什么只是一部分。
int ScreenWith = screen.Bounds.Width;
int ScreenHeight = screen.Bounds.Height;
Graphics g = this.CreateGraphics();
//创建一个以当前屏幕为模板的图象
Image myimage = new Bitmap(ScreenWith, ScreenHeight, g);
//创建以屏幕大小为标准的位图
Graphics gg = Graphics.FromImage(myimage);
IntPtr dc = g.GetHdc();//得到屏幕的DC
IntPtr dcdc = gg.GetHdc();//得到Bitmap的DC
BitBlt(dcdc, 0, 0, ScreenWith, ScreenHeight, dc, 0, 0, 13369376);
//调用此API函数,实现屏幕捕获
g.ReleaseHdc(dc);//释放掉屏幕的DC
gg.ReleaseHdc(dcdc);//释放掉Bitmap的DC
myimage.Save(Application.StartupPath + @"\截屏.jpg", ImageFormat.Jpeg);
this.Show();
MessageBox.Show("截屏成功!");
this.Close();
得到的为什么只是一部分。
是不是这一句Graphics g = this.CreateGraphics();导致获得的屏幕DC的原点不再左上角
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Collections;
using System.Drawing.Imaging;
using System.Threading;
[DllImport("gdi32.dll")]
private static extern int BitBlt(IntPtr hdcDest,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hdcSrc,int nXSrc,int nYSrc,UInt32 dwRop);
{
this.Hide();//如果你不想截取的图象中有此应用程序
Thread.Sleep(1000);
Rectangle rect = new Rectangle();
rect = Screen.GetWorkingArea(this);//获得当前屏幕的大小
Graphics g = this.CreateGraphics();
//创建一个以当前屏幕为模板的图象
Image myimage = new Bitmap(rect.Width, rect.Height, g);
//第二种得到全屏坐标的方法
// Image myimage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,g);
//创建以屏幕大小为标准的位图
Graphics gg = Graphics.FromImage(myimage);
IntPtr dc = g.GetHdc();//得到屏幕的DC
IntPtr dcdc = gg.GetHdc();//得到Bitmap的DC
BitBlt(dcdc, 0, 0, rect.Width, rect.Height, dc, 0, 0, 13369376);
//调用此API函数,实现屏幕捕获
g.ReleaseHdc(dc);//释放掉屏幕的DC
gg.ReleaseHdc(dcdc);//释放掉Bitmap的DC
myimage.Save(Application.StartupPath + @"\bob.jpg", ImageFormat.Jpeg);//以JPG文件格式来保存
this.Show();
}
HWND hWnd = GetDesktopWindow();//获得屏幕的HWND.
HDC hScreenDC = GetDC(hWnd); //获得屏幕的HDC.
这样既可。Bitmap b = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(b);
g.CopyFromScreen(new Point(), new Point(), Screen.PrimaryScreen.Bounds.Size);
b.Save("screen.bmp");