最近在做的一個項目,用的地圖控件比較特殊,
在地圖上面自己繪制的東東沒辦法隨地圖一起導出成圖片
只能繞個彎,用截屏的方式了
一開始用的是這樣:
- /// <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;
- }