private static Bitmap capture(WebBrowser source, IntPtr hwnd) { HtmlDocument htmlDocument = source.Document; HtmlElement htmlElement = htmlDocument.Body; Rectangle rectangle = new Rectangle(new Point(0, 0), htmlElement.ScrollRectangle.Size); Bitmap image = new Bitmap(rectangle.Size.Width, rectangle.Size.Height, Graphics.FromHwnd(hwnd)); using (Graphics graphics = Graphics.FromImage(image)) { IOleObject oleObject = (IOleObject)htmlDocument.DomDocument; if (oleObject != null) { IntPtr imageDC = graphics.GetHdc(); IntPtr pUnk = Marshal.GetIUnknownForObject(source.ActiveXInstance); try { Size currentSize = new Size(); oleObject.GetExtent(DVASPECT.DVASPECT_CONTENT, out currentSize); Size drawingSize = convertPixelToHIMETRIC(rectangle.Size, imageDC); oleObject.SetExtent(DVASPECT.DVASPECT_CONTENT, ref drawingSize); OleDraw(pUnk, DVASPECT.DVASPECT_CONTENT, imageDC, ref rectangle); oleObject.SetExtent(DVASPECT.DVASPECT_CONTENT, ref currentSize); } finally { Marshal.Release(pUnk); graphics.ReleaseHdc(imageDC); } } graphics.Dispose(); } return(image); }
/// <summary> /// コンテンツキャプチャ /// </summary> private void GetCaptureImageInternal() { if (InvokeRequired) { // 別スレッドから呼び出された場合 Invoke(new GetCaptureImageInternalDelegate(GetCaptureImageInternal)); return; } Bitmap rt = null; Size curSize; try { #region コンテンツのボディー部を取得 HtmlDocument htmlDoc = Document; if (htmlDoc == null) { throw new NullReferenceException(); } HtmlElement element = htmlDoc.Body; if (element == null) { throw new NullReferenceException(); } IOleObject oleObj = (IOleObject)htmlDoc.DomDocument; if (oleObj == null) { throw new NullReferenceException(); } #endregion #region コンテンツの情報を記憶 // スクロール位置を記憶 Point scrOrg = new Point(); scrOrg.X = element.Parent.ScrollLeft; scrOrg.Y = element.Parent.ScrollTop; #endregion #region コンテンツをキャプチャ用に調整 // 一時的に拡大率を変更する if (zoomNowRate != captureZoomRate) { SHDocVw.IWebBrowser2 browser = (SHDocVw.IWebBrowser2)ActiveXInstance; object izoom = (Int32)captureZoomRate; object ozoom = (Int32)0; browser.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref izoom, ref ozoom); } // コンテンツサイズ計算(スクロールバー分大きくする) oleObj.GetExtent(DVASPECT.DVASPECT_CONTENT, out curSize); Rectangle conRect = new Rectangle(0, 0, (int)(element.ScrollRectangle.Size.Width * captureZoomRate / 100 + 0.5), (int)(element.ScrollRectangle.Size.Height * captureZoomRate / 100 + 0.5) ); conRect.Width += SystemInformation.VerticalScrollBarWidth; conRect.Height += SystemInformation.HorizontalScrollBarHeight; #endregion if (captureDrawMode == CaptureDrawModes.OleDraw) { rt = new Bitmap(conRect.Width, conRect.Height); } else { rt = new Bitmap(this.Size.Width, this.Size.Height); } Graphics g = Graphics.FromImage(rt); IntPtr dc = g.GetHdc(); try { #region コンテンツ全体をキャプチャ switch (captureDrawMode) { case CaptureDrawModes.OleDraw: #region OleDrawによるキャプチャ { IntPtr pUnk = Marshal.GetIUnknownForObject(ActiveXInstance); try { // コンテンツ描画範囲をコンテンツサイズに設定 Size drawSize = PixelToHIMETRIC(conRect.Size, dc); oleObj.SetExtent(DVASPECT.DVASPECT_CONTENT, ref drawSize); // 描画 OleDraw(pUnk, DVASPECT.DVASPECT_CONTENT, dc, ref conRect); } catch (Exception es) { ThreadExceptionDialog ed = new ThreadExceptionDialog(es); ed.ShowDialog(); } finally { Marshal.Release(pUnk); } } #endregion break; case CaptureDrawModes.PrintWindow: #region PrintWindowによるキャプチャ { // 表示サイズで取得 PrintWindow(Handle, dc, 0); } #endregion break; } #endregion } catch (Exception es) { ThreadExceptionDialog ed = new ThreadExceptionDialog(es); ed.ShowDialog(); } finally { g.ReleaseHdc(dc); g.Dispose(); } #region コンテンツを元に戻す // 拡大率とコンテンツサイズを戻す if (zoomNowRate != captureZoomRate) { SHDocVw.IWebBrowser2 browser = (SHDocVw.IWebBrowser2)ActiveXInstance; object izoom = (Int32)zoomNowRate; object ozoom = (Int32)0; browser.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref izoom, ref ozoom); } oleObj.SetExtent(DVASPECT.DVASPECT_CONTENT, ref curSize); // スクロール位置を戻す SetPosition(scrOrg); #endregion } catch (NullReferenceException) { if (rt == null) { rt = new Bitmap(1, 1); } } catch (Exception es) { ThreadExceptionDialog ed = new ThreadExceptionDialog(es); ed.ShowDialog(); } imageOrigin = rt; }