最近在做的一个项目,用的地图控件比较特殊,
在地图上面自己绘制的东东没办法随地图一起导出成图片
只能绕个弯,用截屏的方式了
一开始用的是这样:
/// <summary> /// 截图 /// </summary> /// <returns></returns> private Image CaptureScreen() { Image MyImage = null; //获得当前地图控件的大小 Graphics grapScreen = m_pFremework.Hook.CreateGraphics(); //Rectangle rect = new Rectangle(); //rect = Screen.GetWorkingArea(m_pFremework.Hook);//范围不准确,直接使用控件的属性 Rectangle rect = new Rectangle(0, 0, m_pFremework.Hook.Width, m_pFremework.Hook.Height); MyImage = new Bitmap(rect.Width, rect.Height, grapScreen); Graphics grapImage = Graphics.FromImage(MyImage); IntPtr dc1 = grapScreen.GetHdc(); IntPtr dc2 = grapImage.GetHdc(); BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0, 0, 13369376); //释放掉DC grapScreen.ReleaseHdc(dc1); grapImage.ReleaseHdc(dc2); return MyImage; }
这个函数在win7中没有任何问题,但在xp中发现,任何遮挡到地图控件的窗体都被截下来了
网上搜搜,发现了一个PrintWindow API可以解决这个问题,用下来发现这个只能截整个窗体,
无法单独截取地图控件,这个就简单了,直接使用BitMap的Clone函数裁剪出来地图控件部分就搞定了,
代码是这个样子:
private Image CaptureScreen2() { Image MyImage = null; IntPtr hdc = GetWindowDC(m_pFremework.MdiForm.Handle); LPPOINT lphook = new LPPOINT(); lphook.x = 0; lphook.y = 0; ClientToScreen(m_pFremework.Hook.Handle, ref lphook); ScreenToClient(m_pFremework.MdiForm.Handle, ref lphook); Rectangle rect = new Rectangle(lphook.x, lphook.y, m_pFremework.Hook.Width, m_pFremework.Hook.Height); if (hdc != IntPtr.Zero) { IntPtr hdcMem = CreateCompatibleDC(hdc); if (hdcMem != IntPtr.Zero) { IntPtr hbitmap = CreateCompatibleBitmap(hdc, m_pFremework.MdiForm.Width, m_pFremework.MdiForm.Height); if (hbitmap != IntPtr.Zero) { IntPtr ip = SelectObject(hdcMem, hbitmap); if (ip != IntPtr.Zero) { bool a = PrintWindow(m_pFremework.MdiForm.Handle, hdcMem, 1); DeleteObject(hbitmap); Image tempImg = Image.FromHbitmap(hbitmap); Bitmap b = new Bitmap(tempImg); MyImage = b.Clone(rect, b.PixelFormat); } } } } return MyImage; }
==============================分割线==============================
以下是用到的一些windows系统api
==============================分割线==============================
[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 // 光栅的处理数值 ); [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern IntPtr GetWindowDC( IntPtr hwnd ); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern IntPtr CreateCompatibleDC( IntPtr hdc ); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern IntPtr CreateCompatibleBitmap( IntPtr hdc, int nWidth, // width of bitmap, in pixels int nHeight // height of bitmap, in pixels ); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern IntPtr SelectObject( IntPtr hdc,// handle to DC IntPtr hgdiobj // handle to object ); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern bool DeleteObject( IntPtr hObject // handle to graphic object ); [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern bool PrintWindow( IntPtr hwnd, // Window to copy IntPtr hdcBlt, // HDC to print into int nFlags // Optional flags ); [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern int ReleaseDC( IntPtr hWnd, // handle to window IntPtr hDC // handle to DC ); [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern bool ScreenToClient( IntPtr hWnd, // handle to window ref LPPOINT lpPoint // screen coordinates ); [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern bool ClientToScreen( IntPtr hWnd, // handle to window ref LPPOINT lpPoint // screen coordinates ); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct LPPOINT { public int x; public int y; } }
参考网址:http://zhidao.baidu.com/question/29867112.html?fr=qrl&cid=869&index=1