原文地址:http://blog.csdn.net/u013096568/article/details/53400389
panel上可以通過DrawToBitmap截圖,不管是否在屏幕外是否有遮擋
Bitmap sourceBitmap = new Bitmap(400, 300); //Control ct = frmMain.mianForm.panel1 as Control; //ct.DrawToBitmap(sourceBitmap, new System.Drawing.Rectangle(0, 0, 400, 300)); panel1.DrawToBitmap(sourceBitmap, new System.Drawing.Rectangle(0, 0, 400, 300)); sourceBitmap.Save(@"e:\123form2.bmp");
在圖片上打水印
string strpath = @"e:\1.bmp"; Bitmap bmp = new Bitmap(strpath); Graphics graphics = Graphics.FromImage(bmp); System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); graphics.FillEllipse(myBrush, new System.Drawing.Rectangle(150, 200, 10, 10));//畫圓 try { bmp.Save(@"e:\new.bmp"); } catch (Exception ex) { MessageBox.Show(ex.Message); }
抓屏截圖
Image objImage = new Bitmap(400, 300); Graphics g = Graphics.FromImage(objImage); g.CopyFromScreen(new System.Drawing.Point(Cursor.Position.X - 150, Cursor.Position.Y - 25), new System.Drawing.Point(0, 0), new Size(400, 300)); IntPtr dc1 = g.GetHdc(); g.ReleaseHdc(dc1); objImage.Save("e:\\test.jpg");
非頂端窗口截圖
用Windows熱鍵截圖然后保存的我就不說了,地球人都知道.
如何截取非前端窗體的截圖,要先獲取所要截圖的窗口的句柄IntPtr PicWindow = this.Handle
首先說一下PrintWindow這個API的使用
public static Bitmap GetWindowCapture(IntPtr hWnd) { IntPtr hscrdc = GetWindowDC(hWnd); RECT windowRect = new RECT(); GetWindowRect(hWnd, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height); IntPtr hmemdc = CreateCompatibleDC(hscrdc); SelectObject(hmemdc, hbitmap); PrintWindow(hWnd, hmemdc, 0); Bitmap bmp = Bitmap.FromHbitmap(hbitmap); DeleteDC(hscrdc);//刪除用過的對象 DeleteDC(hmemdc);//刪除用過的對象 return bmp; } [DllImport("user32.dll")] public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); [DllImport("gdi32.dll")] public static extern IntPtr CreateDC( string lpszDriver, // driver name驅動名 string lpszDevice, // device name設備名 string lpszOutput, // not used; should be NULL IntPtr lpInitData // optional printer data ); [DllImport("gdi32.dll")] public static extern int BitBlt( IntPtr hdcDest, // handle to destination DC目標設備的句柄 int nXDest, // x-coord of destination upper-left corner目標對象的左上角的X坐標 int nYDest, // y-coord of destination upper-left corner目標對象的左上角的Y坐標 int nWidth, // width of destination rectangle目標對象的矩形寬度 int nHeight, // height of destination rectangle目標對象的矩形長度 IntPtr hdcSrc, // handle to source DC源設備的句柄 int nXSrc, // x-coordinate of source upper-left corner源對象的左上角的X坐標 int nYSrc, // y-coordinate of source upper-left corner源對象的左上角的Y坐標 UInt32 dwRop // raster operation code光柵的操作值 ); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC( IntPtr hdc // handle to DC ); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleBitmap( IntPtr hdc, // handle to DC int nWidth, // width of bitmap, in pixels int nHeight // height of bitmap, in pixels ); [DllImport("gdi32.dll")] public static extern IntPtr SelectObject( IntPtr hdc, // handle to DC IntPtr hgdiobj // handle to object ); [DllImport("gdi32.dll")] public static extern int DeleteDC( IntPtr hdc // handle to DC ); [DllImport("user32.dll")] public static extern bool PrintWindow( IntPtr hwnd, // Window to copy,Handle to the window that will be copied. IntPtr hdcBlt, // HDC to print into,Handle to the device context. UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values. ); [DllImport("user32.dll")] public static extern IntPtr GetWindowDC( IntPtr hwnd );
很遺憾,上面的確可以截取非前端窗體的截圖,但是非GDI的程序是無法截圖的比如DirectX
下面說一下BitBlt這個API的使用
/// <summary> /// 提供全屏和指定窗口的截圖 以及保存為文件的類 /// </summary> public class ScreenCapture { /// <summary> /// 全屏截圖 /// </summary> /// <returns></returns> public Image CaptureScreen() { return CaptureWindow(User32.GetDesktopWindow()); } /// <summary> /// 指定窗口截圖 /// </summary> /// <param name="handle">窗口句柄. (在windows應用程序中, 從Handle屬性獲得)</param> /// <returns></returns> public Image CaptureWindow(IntPtr handle) { IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest);