最近在做的一个项目,用的地图控件比较特殊,
在地图上面自己绘制的东东没办法随地图一起导出成图片
只能绕个弯,用截屏的方式了
一开始用的是这样:
/// <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; }