代码:
//创建显示器的DC 
IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ; 
//由一个指定设备的句柄创建一个新的Graphics对象 
Graphics g1 = Graphics.FromHdc ( dc1 ) ; 
//根据屏幕大小创建一个与之相同大小的Bitmap对象 
Bitmap MyImage = new Bitmap ( Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , g1 ) ; 
Graphics g2 = Graphics.FromImage ( MyImage ) ;  //获得屏幕的句柄 
IntPtr dc3 = g1.GetHdc ( ) ; 
//获得位图的句柄 
IntPtr dc2 = g2.GetHdc ( ) ; 
BitBlt ( dc2 , 0 , 0 , Screen.PrimaryScreen.Bounds.Width , Screen.PrimaryScreen.Bounds.Height , dc3 , 0 , 0 , 13369376 ) ; //把当前屏幕捕获到位图对象中 
//MyImage.Save ( "c:\\MyJpeg.jpg" , ImageFormat.Jpeg ) ; 
//MessageBox.Show ( "已经把当前屏幕保存到C:\\MyJpeg.jpg文件中!" ) ;
//释放屏幕句柄 
g1.ReleaseHdc ( dc3 ) ; 
//释放位图句柄 
g2.ReleaseHdc ( dc2 ) ; /////////////画上鼠标:主要代码///////////////////////////////////
Graphics g3 = Graphics.FromImage ( MyImage );
Cursor.Current.Draw(g3,new Rectangle(Cursor.Position,Cursor.Current.Size));
////////////////////////////////////////////////////////
g3.Flush();
g3.ReleaseHdc(g3.GetHdc());API:
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ] 
private 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 // 光栅的操作值 
) ; 
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ] 
private static extern IntPtr CreateDC ( 
string lpszDriver , // 驱动名称 
string lpszDevice , // 设备名称 
string lpszOutput , // 无用,可以设定位"NULL" 
IntPtr lpInitData // 任意的打印机数据 
) ; /*///////////////////////////////////////////////////////////////////////
问题:
每次画出来的光标都是沙漏状,所以我估计这个Cursor得到的应该不是系统光标,而是程序光标。
请问怎样才能画出正常的光标?
*////////////////////////////////////////////////////////////////////////

解决方案 »

  1.   

    这是我根据你写的而写的,我的显示正常,没有问题我是直接DRAW的,和你的不一样
    //Draw cursor.
    Cursor.Current.Draw(grfxBmp, new Rectangle(Cursor.Position, Cursor.Size));
    全部代码:
            private void ScreenCapture()
            {
                //this.WindowState = FormWindowState.Maximized;            IntPtr dcScreen = CreateDC("DISPLAY", null, null, (IntPtr)null);
                Graphics grfxScreen = Graphics.FromHdc(dcScreen);            Bitmap bmpScreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, grfxScreen);
                Graphics grfxBmp = Graphics.FromImage(bmpScreen);            //because the grahics are being using,so we must get their dc.
                IntPtr dcgscreen = grfxScreen.GetHdc();
                IntPtr dcgbmp = grfxBmp.GetHdc();            //copy the  screen picture to bitmap.
                BitBlt(dcgbmp, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,
                                        dcgscreen, 0, 0, 13369376);            //release all the resources.must be release them before bitblt method.
                grfxBmp.ReleaseHdc(dcgbmp);
                grfxScreen.ReleaseHdc(dcgscreen);
                //Draw cursor.
                Cursor.Current.Draw(grfxBmp, new Rectangle(Cursor.Position, Cursor.Size));
                //save the picuture.
                bmpScreen.Save("Screen Capture.bmp", ImageFormat.Bmp);            //release the resources
                grfxBmp.Dispose();
                grfxScreen.Dispose();        }