private void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { var browser = sender as WebBrowser; Rectangle body = browser.Document.Body.ScrollRectangle; browser.Width = body.Width; browser.Height = body.Height; using (var bitmap = new Bitmap(body.Width, body.Height)) { IViewObject ivo = browser.Document.DomDocument as IViewObject; using (Graphics g = Graphics.FromImage(bitmap)) { //get the handle to the device context and draw IntPtr hdc = g.GetHdc(); ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref body, ref body, IntPtr.Zero, 0); g.ReleaseHdc(hdc); } if (_captured != null) { WebPageCapturedEventArgs args = new WebPageCapturedEventArgs() { Image = new DefaultImage(bitmap) }; _captured(this, args); } } }
/// <summary> /// Captures an image representation of browser contents. /// </summary> /// <param name="browser">The WebBrowser instance rendering the SVG.</param> /// <param name="rectangle">The client rectangle to capture from.</param> /// <param name="backgroundColor">The default background color to apply.</param> /// <returns>A Bitmap representing the browser contents.</returns> public static Bitmap GetBrowserContentImage(WebBrowser browser, Rectangle rectangle, Color backgroundColor) { Bitmap image = new Bitmap(rectangle.Width, rectangle.Height); using (Graphics graphics = Graphics.FromImage(image)) { IntPtr deviceContextHandle = IntPtr.Zero; RECT rect = new RECT { Left = rectangle.Left, Top = rectangle.Top, Right = rectangle.Right, Bottom = rectangle.Bottom, }; graphics.Clear(backgroundColor); try { deviceContextHandle = graphics.GetHdc(); IViewObject viewObject = browser?.ActiveXInstance as IViewObject; viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rect, IntPtr.Zero, IntPtr.Zero, 0); } finally { if (deviceContextHandle != IntPtr.Zero) { graphics.ReleaseHdc(deviceContextHandle); } } } return(image); }
void tready_Tick(object sender, EventArgs e) { try { //stop the timer tready.Stop(); mshtml.IHTMLDocument2 docs2 = (mshtml.IHTMLDocument2)web.Document.DomDocument; mshtml.IHTMLDocument3 docs3 = (mshtml.IHTMLDocument3)web.Document.DomDocument; mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2)docs2.body; mshtml.IHTMLElement2 root2 = (mshtml.IHTMLElement2)docs3.documentElement; // Determine dimensions for the image; we could add minWidth here // to ensure that we get closer to the minimal width (the width // computed might be a few pixels less than what we want). int width = Math.Max(body2.scrollWidth, root2.scrollWidth); int height = Math.Max(root2.scrollHeight, body2.scrollHeight); //get the size of the document's body Rectangle docRectangle = new Rectangle(0, 0, width, height); web.Width = docRectangle.Width; web.Height = docRectangle.Height; //if the imgsize is null, the size of the image will //be the same as the size of webbrowser object //otherwise set the image size to imgsize Rectangle imgRectangle; if (imgsize == null) { imgRectangle = docRectangle; } else { imgRectangle = new Rectangle() { Location = new Point(0, 0), Size = imgsize.Value } }; //create a bitmap object Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height); //get the viewobject of the WebBrowser IViewObject ivo = web.Document.DomDocument as IViewObject; using (Graphics g = Graphics.FromImage(bitmap)) { //get the handle to the device context and draw IntPtr hdc = g.GetHdc(); ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref docRectangle, IntPtr.Zero, 0); g.ReleaseHdc(hdc); } //invoke the HtmlImageCapture event bitmap.Save(fileName); bitmap.Dispose(); } catch { //System.Diagnostics.Process.GetCurrentProcess().Kill(); } if (HtmlImageCapture != null) { HtmlImageCapture(this, web.Url); } }
private static Image GetRealScreenshot(int width, int height, IViewObject viewObject) { try { var image = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); var rect = new RECT { left = 0, top = 0, right = width, bottom = height }; var tdevice = new DVTARGETDEVICE { tdSize = 0, }; using (var graphics = Graphics.FromImage(image)) { var hdc = graphics.GetHdc(); viewObject.Draw(1, 0, IntPtr.Zero, tdevice, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); graphics.ReleaseHdc(hdc); } return(image); } catch { return(null); } }
public static void GetImage(object obj, Image destination, Color backgroundColor) { using (Graphics graphics = Graphics.FromImage(destination)) { IntPtr deviceContextHandle = IntPtr.Zero; RECT rectangle = new RECT(); rectangle.Right = destination.Width; rectangle.Bottom = destination.Height; graphics.Clear(backgroundColor); try { deviceContextHandle = graphics.GetHdc(); IViewObject viewObject = obj as IViewObject; viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0); } finally { if (deviceContextHandle != IntPtr.Zero) { graphics.ReleaseHdc(deviceContextHandle); } } } }
public void DoCapture() { IHTMLDocument2 doc2 = (IHTMLDocument2)mWb.Document; IHTMLDocument3 doc3 = (IHTMLDocument3)mWb.Document; IHTMLElement2 body2 = (IHTMLElement2)doc2.body; IHTMLElement2 root2 = (IHTMLElement2)doc3.documentElement; // Determine dimensions for the image; we could add minWidth here // to ensure that we get closer to the minimal width (the width // computed might be a few pixels less than what we want). int width = Math.Max(body2.scrollWidth, root2.scrollWidth); int height = Math.Max(root2.scrollHeight, body2.scrollHeight); // Resize the web browser control mWb.SetBounds(0, 0, width, height); // Do it a second time; in some cases the initial values are // off by quite a lot, for as yet unknown reasons. We could // also do this in a loop until the values stop changing with // some additional terminating condition like n attempts. width = Math.Max(body2.scrollWidth, root2.scrollWidth); height = Math.Max(root2.scrollHeight, body2.scrollHeight); //@todo override with fixed values Rectangle resolution = System.Windows.Forms.Screen.PrimaryScreen.Bounds; width = resolution.Width; height = resolution.Height; mWb.SetBounds(0, 0, width, height); Bitmap image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); _RECTL bounds; bounds.left = 0; bounds.top = 0; bounds.right = width; bounds.bottom = height; IntPtr hdc = g.GetHdc(); IViewObject iv = doc2 as IViewObject; // TODO: Write to Metafile instead if requested. iv.Draw(1, -1, (IntPtr)0, (IntPtr)0, (IntPtr)0, (IntPtr)hdc, ref bounds, (IntPtr)0, (IntPtr)0, 0); g.ReleaseHdc(hdc); image.Save(mFile); image.Dispose(); if (mImageDiff != null) { mImageDiff.run(); } }
void tready_Tick(object sender, EventArgs e) { //stop the timer tready.Stop(); //get the size of the document's body Rectangle body = web.Document.Body.ScrollRectangle; //check if the document width/height is greater than screen width/height Rectangle docRectangle = new Rectangle() { Location = new Point(0, 0), Size = new Size(body.Width > screen.Width ? body.Width : screen.Width, body.Height > screen.Height ? body.Height : screen.Height) }; //set the width and height of the WebBrowser object web.Width = docRectangle.Width; web.Height = docRectangle.Height; //if the imgsize is null, the size of the image will //be the same as the size of webbrowser object //otherwise set the image size to imgsize Rectangle imgRectangle; if (imgsize == null) { imgRectangle = docRectangle; } else { imgRectangle = new Rectangle() { Location = new Point(0, 0), Size = imgsize.Value } }; //create a bitmap object Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height); //get the viewobject of the WebBrowser IViewObject ivo = web.Document.DomDocument as IViewObject; using (Graphics g = Graphics.FromImage(bitmap)) { //get the handle to the device context and draw IntPtr hdc = g.GetHdc(); ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref docRectangle, IntPtr.Zero, 0); g.ReleaseHdc(hdc); } //invoke the HtmlImageCapture event HtmlImageCapture(this, web.Url, bitmap); } #endregion }
/// <summary> /// The <see cref="Form.Shown"/> event handler. /// Makes a screenshot of the flash movie in its initial state. /// </summary> /// <param name="sender">Source of the event.</param> /// <param name="e">An empty <see cref="EventArgs"/></param> private void FlashCapture_Shown(object sender, EventArgs e) { // Grab IViewObject interface from the ocx. IViewObject viewObject = (IViewObject)this.flashObject.GetOcx(); // Create empty thumb bitmap Image thumb = new Bitmap(this.Width, this.Height); thumb.Tag = this.movieFile; // Check for success if (viewObject == null) { this.screenShot = thumb; return; } // Create graphics for bitmap using (Graphics graphics = Graphics.FromImage(thumb)) { // draw int hr = -1; // Set up RECTL structure Microsoft.VisualStudio.OLE.Interop.RECTL bounds = new Microsoft.VisualStudio.OLE.Interop.RECTL(); bounds.left = 0; bounds.top = 0; bounds.right = this.Width; bounds.bottom = this.Height; // get hdc IntPtr hdc = graphics.GetHdc(); // Draw hr = viewObject.Draw( Microsoft.VisualStudio.OLE.Interop.DVASPECT.DVASPECT_CONTENT, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref bounds, ref bounds, IntPtr.Zero, (uint)0); // Release HDC graphics.ReleaseHdc(hdc); } this.screenShot = thumb; this.Close(); }
public Image Ole2Image(int width, int height) { Bitmap result = null; IRichEditOle richEditInterface = this.GetRichEditOleInterface(); if (richEditInterface.GetObjectCount() == 0) { return(result); } REOBJECT reObject = new REOBJECT(); // S_OK == 0, so 0 == success. if (0 == richEditInterface.GetObject(0, reObject, GetObjectOptions.REO_GETOBJ_POLEOBJ)) { try { IntPtr pViewObject = IntPtr.Zero; Guid guid = new Guid("{0000010d-0000-0000-C000-000000000046}"); Marshal.QueryInterface(reObject.poleobj, ref guid, out pViewObject); IViewObject viewobject = (IViewObject)Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(IViewObject)); int imgwidth = width; int imgheight = System.Convert.ToInt32(reObject.sizel.y * 1.0 / reObject.sizel.x * imgwidth); result = new Bitmap(imgwidth, imgheight); Rectangle imgRectangle = new Rectangle(0, 0, imgwidth, imgheight); using (Graphics g = Graphics.FromImage(result)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; IntPtr hdc = g.GetHdc(); viewobject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref imgRectangle, IntPtr.Zero, 0); g.ReleaseHdc(hdc); g.DrawRectangle(Pens.White, imgRectangle); } //Release the pViewObject Marshal.Release(pViewObject); } finally { Marshal.Release(reObject.poleobj); } } return(result); }
private static Bitmap GetScreenshot(IViewObject viewObject, int width, int height) { var image = new Bitmap(width, height, PixelFormat.Format24bppRgb); var rect = new RECT { left = 0, top = 0, width = width, height = height, }; var tdevice = new DVTARGETDEVICE { tdSize = 0, }; using (var graphics = Graphics.FromImage(image)) { var hdc = graphics.GetHdc(); viewObject.Draw(1, 0, IntPtr.Zero, tdevice, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); graphics.ReleaseHdc(hdc); } return(image); }
public static Bitmap TakeSnapshot(IViewObject obj, int width, int height) { // draw the view on a Bitmap IntPtr hBitmapDC = IntPtr.Zero; IntPtr hBitmap = IntPtr.Zero; IntPtr hPreviousObject = IntPtr.Zero; Bitmap bitmap = null; try { // create GDI objects used for drawing hBitmapDC = Gdi32.CreateCompatibleDC(User32.GetDC(IntPtr.Zero)); hBitmap = Gdi32.CreateCompatibleBitmap(User32.GetDC(IntPtr.Zero), width, height); hPreviousObject = Gdi32.SelectObject(hBitmapDC, hBitmap); RECT sourceRect = new RECT(); sourceRect.right = width; sourceRect.bottom = height; // draw the bitmap obj.Draw(DVASPECT.CONTENT, 1, IntPtr.Zero, IntPtr.Zero, User32.GetDC(IntPtr.Zero), hBitmapDC, ref sourceRect, IntPtr.Zero, IntPtr.Zero, 0); // convert to a managed bitmap bitmap = Bitmap.FromHbitmap(hBitmap); } finally { // restore previous object Gdi32.SelectObject(hBitmapDC, hPreviousObject); if (hBitmapDC != IntPtr.Zero) { Gdi32.DeleteDC(hBitmapDC); } if (hBitmap != IntPtr.Zero) { Gdi32.DeleteObject(hBitmap); } } return(bitmap); }
private static void TakeScreenshot(int width, int height, IViewObject viewObject, string path) { var image = new Bitmap(width, height, PixelFormat.Format24bppRgb); var rect = new RECT { left = 0, top = 0, width = width, height = height, }; var tdevice = new DVTARGETDEVICE { tdSize = 0, }; using (var graphics = Graphics.FromImage(image)) { var hdc = graphics.GetHdc(); viewObject.Draw(1, 0, IntPtr.Zero, tdevice, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); graphics.ReleaseHdc(hdc); } var format = Path.GetExtension(path) == ".jpg" ? ImageFormat.Jpeg : ImageFormat.Png; image.Save(path, format); }
private void CaptureToFile(WebBrowser webBrowser, string url, HttpContext context, int tenant) { using (var image = new Bitmap(webBrowser.Width, webBrowser.Height)) using (var graphics = Graphics.FromImage(image)) { graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; var hdc = graphics.GetHdc(); IViewObject viewObject = null; try { viewObject = (IViewObject)webBrowser.Document.DomDocument; viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, (COMRECT)webBrowser.ClientRectangle, null, IntPtr.Zero, 0); } finally { if (viewObject != null) { Marshal.ReleaseComObject(viewObject); } graphics.ReleaseHdc(hdc); } var smallSize = BookmarkingSettings.ThumbSmallSize; using (MemoryStream ms = new MemoryStream()) { image.GetThumbnailImage(smallSize.Width, smallSize.Height, null, IntPtr.Zero) .Save(ms, BookmarkingSettings.CaptureImageFormat); ms.Position = 0; IDataStore store = getStore(context, tenant); var fileName = GetFileNameByUrl(url, smallSize); store.Save(string.Empty, fileName, ms); } } }
/// <summary> /// スクリーンショットを保存します。 /// </summary> /// <param name="path">保存先。</param> /// <param name="format">画像のフォーマット。</param> private void SaveScreenShot(string path, System.Drawing.Imaging.ImageFormat format) { var wb = Browser; if (!IsKanColleLoaded) { AddLog(3, string.Format("艦これが読み込まれていないため、スクリーンショットを撮ることはできません。")); System.Media.SystemSounds.Beep.Play(); return; } try { IViewObject viewobj = null; //int width = 0, height = 0; if (wb.Document.Url.ToString().Contains(".swf?")) { viewobj = wb.Document.GetElementsByTagName("embed")[0].DomElement as IViewObject; if (viewobj == null) { throw new InvalidOperationException("embed 要素の取得に失敗しました。"); } //width = ( (HTMLEmbed)viewobj ).clientWidth; //height = ( (HTMLEmbed)viewobj ).clientHeight; } else { var swf = getFrameElementById(wb.Document, "externalswf"); if (swf == null) { throw new InvalidOperationException("対象の swf が見つかりませんでした。"); } Func <dynamic, bool> isvalid = target => { if (target == null) { return(false); } viewobj = target as IViewObject; if (viewobj == null) { return(false); } //if ( !int.TryParse( target.width, out width ) ) return false; //if ( !int.TryParse( target.height, out height ) ) return false; return(true); }; if (!isvalid(swf.DomElement as HTMLEmbed) && !isvalid(swf.DomElement as HTMLObjectElement)) { throw new InvalidOperationException("対象の swf が見つかりませんでした。"); } } if (viewobj != null) { var rect = new RECT { left = 0, top = 0, width = KanColleSize.Width, height = KanColleSize.Height }; using (var image = new Bitmap(rect.width, rect.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { var device = new DVTARGETDEVICE { tdSize = 0 }; using (var g = Graphics.FromImage(image)) { var hdc = g.GetHdc(); viewobj.Draw(1, 0, IntPtr.Zero, device, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); g.ReleaseHdc(hdc); } image.Save(path, format); } } _lastScreenShotPath = path; AddLog(2, string.Format("スクリーンショットを {0} に保存しました。", path)); } catch (Exception ex) { BrowserHost.AsyncRemoteRun(() => BrowserHost.Proxy.SendErrorReport(ex.ToString(), "スクリーンショットの保存時にエラーが発生しました。")); System.Media.SystemSounds.Beep.Play(); } }
private static void TakeScreenshot(int width, int height, IViewObject viewObject, string path) { var image = new Bitmap(width, height, PixelFormat.Format24bppRgb); var rect = new RECT { left = 0, top = 0, width = width, height = height, }; var tdevice = new DVTARGETDEVICE { tdSize = 0, }; if (viewObject != null) { using (var graphics = Graphics.FromImage(image)) { var hdc = graphics.GetHdc(); viewObject.Draw(1, 0, IntPtr.Zero, tdevice, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); graphics.ReleaseHdc(hdc); } } else { //From GrandcypherGear TakeScreenShot rect.height = Convert.ToInt32(rect.height * Setting.Current.BrowserZoomFactorPercentage / 100); rect.width = Convert.ToInt32(rect.width * Setting.Current.BrowserZoomFactorPercentage / 100); image = new Bitmap(rect.width, rect.height, PixelFormat.Format24bppRgb); KanColleViewer.Views.MainWindow.Current.SetPos(); int left = KanColleViewer.Views.MainWindow.Current.rect.left; int top = KanColleViewer.Views.MainWindow.Current.rect.top + 91; if (Setting.Current.OrientationMode == OrientationType.Vertical) { top = top - 55; } using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) { float dpiX = graphics.DpiX; float dpiY = graphics.DpiY; if (dpiX != 96f) { left = Convert.ToInt32(left * dpiX / 96f); } if (dpiY != 96f) { top = Convert.ToInt32(top * dpiY / 96f); } } if (left < 0) { left = 0; } using (var graphics = Graphics.FromImage(image)) { graphics.CopyFromScreen(new System.Drawing.Point(left, top), new System.Drawing.Point(0, 0), new System.Drawing.Size(rect.width, rect.height)); } } var format = Path.GetExtension(path) == ".jpg" ? ImageFormat.Jpeg : ImageFormat.Png; image.Save(path, format); if (viewObject == null) { throw new Exception("강제캡처모드로 스크린샷을 작성합니다."); } }
/// <summary> /// スクリーンショットを撮影します。 /// </summary> /// <param name="is32bpp"></param> /// <returns></returns> private Bitmap TakeScreenShot(bool is32bpp) { var wb = Browser; if (!IsKanColleLoaded) { AddLog(3, string.Format("艦これが読み込まれていないため、スクリーンショットを撮ることはできません。")); System.Media.SystemSounds.Beep.Play(); return(null); } try { IViewObject viewobj = null; //int width = 0, height = 0; if (wb.Document.Url.ToString().Contains(".swf?")) { viewobj = wb.Document.GetElementsByTagName("embed")[0].DomElement as IViewObject; if (viewobj == null) { throw new InvalidOperationException("embed 要素の取得に失敗しました。"); } //width = ( (HTMLEmbed)viewobj ).clientWidth; //height = ( (HTMLEmbed)viewobj ).clientHeight; } else { var swf = getFrameElementById(wb.Document, "externalswf"); if (swf == null) { throw new InvalidOperationException("対象の swf が見つかりませんでした。"); } Func <dynamic, bool> isvalid = target => { if (target == null) { return(false); } viewobj = target as IViewObject; if (viewobj == null) { return(false); } //if ( !int.TryParse( target.width, out width ) ) return false; //if ( !int.TryParse( target.height, out height ) ) return false; return(true); }; if (!isvalid(swf.DomElement as HTMLEmbed) && !isvalid(swf.DomElement as HTMLObjectElement)) { throw new InvalidOperationException("対象の swf が見つかりませんでした。"); } } if (viewobj != null) { var rect = new RECT { left = 0, top = 0, width = KanColleSize.Width, height = KanColleSize.Height }; // twitter の劣化回避を行う場合は32ビットの色深度で作業する var image = new Bitmap(rect.width, rect.height, is32bpp ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb); var device = new DVTARGETDEVICE { tdSize = 0 }; using (var g = Graphics.FromImage(image)) { var hdc = g.GetHdc(); viewobj.Draw(1, 0, IntPtr.Zero, device, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); g.ReleaseHdc(hdc); } if (is32bpp) { // 不透明ピクセルのみだと jpeg 化されてしまうため、1px だけわずかに透明にする Color temp = image.GetPixel(image.Width - 1, image.Height - 1); image.SetPixel(image.Width - 1, image.Height - 1, Color.FromArgb(252, temp.R, temp.G, temp.B)); } return(image); } } catch (Exception ex) { SendErrorReport(ex.ToString(), "スクリーンショットの撮影時にエラーが発生しました。"); System.Media.SystemSounds.Beep.Play(); } return(null); }
private static Bitmap GetScreenshot(IViewObject viewObject, int width, int height) { var image = new Bitmap(width, height, PixelFormat.Format24bppRgb); var rect = new RECT { left = 0, top = 0, width = width, height = height, }; var tdevice = new DVTARGETDEVICE { tdSize = 0, }; using (var graphics = Graphics.FromImage(image)) { var hdc = graphics.GetHdc(); viewObject.Draw(1, 0, IntPtr.Zero, tdevice, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); graphics.ReleaseHdc(hdc); } return image; }
//Working for most websites now, but not all //Many thanks to mariscn: http://www.codeproject.com/KB/graphics/html2image.aspx private static Bitmap GenerateScreenshot(this WebBrowser webBrowser, Size?size) { Application.DoEvents(); Control parent = webBrowser.Parent; DockStyle dockStyle = webBrowser.Dock; bool scrollbarsEnabled = webBrowser.ScrollBarsEnabled; if (parent != null) { parent.Controls.Remove(webBrowser); } Rectangle screen = Screen.PrimaryScreen.Bounds; Size? imageSize = null; Rectangle body = webBrowser.Document.Body.ScrollRectangle; //check if the document width/height is greater than screen width/height Rectangle docRectangle = new Rectangle() { Location = new Point(0, 0), Size = new Size( body.Width > screen.Width ? body.Width : screen.Width, body.Height > screen.Height ? body.Height : screen.Height) }; //set the width and height of the WebBrowser object webBrowser.ScrollBarsEnabled = false; webBrowser.Size = new Size(docRectangle.Width, docRectangle.Height); //Cannot update scrollrectangle. Wehn web browser size changed, only width of body changed; not height //webBrowser.Document.Body.ScrollRectangle.Width = webBrowser.Width; //webBrowser.Document.Body.ScrollRectangle.Height = webBrowser.Height; //if the imgsize is null, the size of the image will //be the same as the size of webbrowser object //otherwise set the image size to imgsize Rectangle imgRectangle; if (imageSize == null) { imgRectangle = docRectangle; } else { imgRectangle = new Rectangle() { Location = new Point(0, 0), Size = imageSize.Value } }; //create a bitmap object Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height); //get the viewobject of the WebBrowser IViewObject viewObject = webBrowser.Document.DomDocument as IViewObject; using (Graphics g = Graphics.FromImage(bitmap)) { //get the handle to the device context and draw IntPtr hdc = g.GetHdc(); viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref docRectangle, IntPtr.Zero, 0); g.ReleaseHdc(hdc); } if (parent != null) { parent.Controls.Add(webBrowser); webBrowser.Dock = dockStyle; } webBrowser.ScrollBarsEnabled = scrollbarsEnabled; return(bitmap); }
private static Bitmap ConverHTML(string htmPath) { string ImagePath = string.Empty; WebBrowser web = new WebBrowser(); web.Navigate(htmPath); while (web.ReadyState != WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); } Rectangle screen = Screen.PrimaryScreen.Bounds; Size? imgsize = null; //set the webbrowser width and hight web.Width = screen.Width; web.Height = screen.Height; //suppress script errors and hide scroll bars web.ScriptErrorsSuppressed = true; web.ScrollBarsEnabled = false; Rectangle body = web.Document.Body.ScrollRectangle; //check if the document width/height is greater than screen width/height Rectangle docRectangle = new Rectangle() { Location = new Point(0, 0), Size = new Size(body.Width > screen.Width ? body.Width : screen.Width, body.Height > screen.Height ? body.Height : screen.Height) }; //set the width and height of the WebBrowser object web.Width = docRectangle.Width; web.Height = docRectangle.Height; //if the imgsize is null, the size of the image will //be the same as the size of webbrowser object //otherwise set the image size to imgsize Rectangle imgRectangle; if (imgsize == null) { imgRectangle = docRectangle; } else { imgRectangle = new Rectangle() { Location = new Point(0, 0), Size = imgsize.Value } }; //create a bitmap object Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height); //get the viewobject of the WebBrowser IViewObject ivo = web.Document.DomDocument as IViewObject; using (Graphics g = Graphics.FromImage(bitmap)) { // Graphics.SmoothingMode属性: 例如SmoothingMode.HighQuality可以产生高质量图片,但是效率低. //Graphics.CompositingQuality 属性: 例如: CompositingQuality.HighQuality也是产生高质量图,效率低下. //Graphics.InterpolationMode 属性,例如: InterpolationMode.HighQualityBicubic与前两个也是同样的效果. //g.SmoothingMode = SmoothingMode.HighQuality; //g.CompositingQuality = CompositingQuality.HighQuality; //g.InterpolationMode = InterpolationMode.HighQualityBicubic; ////去除锯齿 //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; //get the handle to the device context and draw IntPtr hdc = g.GetHdc(); ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref docRectangle, IntPtr.Zero, 0); g.ReleaseHdc(hdc); } return(bitmap); }
public static Bitmap TakeSnapshot(IViewObject obj, int width, int height) { // draw the view on a Bitmap IntPtr hBitmapDC = IntPtr.Zero; IntPtr hBitmap = IntPtr.Zero; IntPtr hPreviousObject = IntPtr.Zero; Bitmap bitmap = null; try { // create GDI objects used for drawing hBitmapDC = Gdi32.CreateCompatibleDC(User32.GetDC(IntPtr.Zero)); hBitmap = Gdi32.CreateCompatibleBitmap(User32.GetDC(IntPtr.Zero), width, height); hPreviousObject = Gdi32.SelectObject(hBitmapDC, hBitmap); RECT sourceRect = new RECT(); sourceRect.right = width; sourceRect.bottom = height; // draw the bitmap obj.Draw(DVASPECT.CONTENT, 1, IntPtr.Zero, IntPtr.Zero, User32.GetDC(IntPtr.Zero), hBitmapDC, ref sourceRect, IntPtr.Zero, IntPtr.Zero, 0); // convert to a managed bitmap bitmap = Bitmap.FromHbitmap(hBitmap); } finally { // restore previous object Gdi32.SelectObject(hBitmapDC, hPreviousObject); if (hBitmapDC != IntPtr.Zero) Gdi32.DeleteDC(hBitmapDC); if (hBitmap != IntPtr.Zero) Gdi32.DeleteObject(hBitmap); } return bitmap; }
/// <summary> /// <see cref="WebBrowser.Document"/> (<see cref="HTMLDocument"/>) から艦これの Flash 要素を特定し、指定したパスにスクリーンショットを保存します。 /// </summary> /// <remarks> /// 本スクリーンショット機能は、「艦これ 司令部室」開発者の @haxe さんより多大なご協力を頂き実装できました。 /// ありがとうございました。 /// </remarks> /// <param name="path"></param> private void SaveCore(string path) { const string notFoundMessage = "艦これの Flash 要素が見つかりません。"; var document = this.AssociatedObject.Document as HTMLDocument; if (document == null) { throw new Exception(notFoundMessage); } var gameFrame = document.getElementById("game_frame").document as HTMLDocument; if (gameFrame == null) { throw new Exception(notFoundMessage); } var frames = document.frames; var find = false; for (var i = 0; i < frames.length; i++) { var item = frames.item(i); var provider = item as IServiceProvider; if (provider == null) { continue; } object ppvObject; provider.QueryService(typeof(IWebBrowserApp).GUID, typeof(IWebBrowser2).GUID, out ppvObject); var webBrowser = ppvObject as IWebBrowser2; if (webBrowser == null) { continue; } var iframeDocument = webBrowser.Document as HTMLDocument; if (iframeDocument == null) { continue; } //flash要素が<embed>である場合と<object>である場合を判別して抽出 IViewObject viewObject = null; int width = 0, height = 0; var swf = iframeDocument.getElementById("externalswf"); if (swf == null) { continue; } Func <dynamic, bool> function = target => { if (target == null) { return(false); } viewObject = target as IViewObject; if (viewObject == null) { return(false); } width = int.Parse(target.width); height = int.Parse(target.height); return(true); }; if (!function(swf as HTMLEmbed) && !function(swf as HTMLObjectElement)) { continue; } find = true; var image = new Bitmap(width, height, PixelFormat.Format24bppRgb); var rect = new RECT { left = 0, top = 0, width = width, height = height, }; var tdevice = new DVTARGETDEVICE { tdSize = 0, }; using (var graphics = Graphics.FromImage(image)) { var hdc = graphics.GetHdc(); viewObject.Draw(1, 0, IntPtr.Zero, tdevice, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); graphics.ReleaseHdc(hdc); } image.Save(path, ImageFormat.Png); break; } if (!find) { throw new Exception(notFoundMessage); } }
private static void SaveScreenshot(int width, int height, IViewObject viewObject, string path) { using (var image = new Bitmap(width, height, PixelFormat.Format24bppRgb)) { var rect = new RECT { left = 0, top = 0, width = width, height = height }; var tdevice = new DVTARGETDEVICE { tdSize = 0 }; using (var graphics = Graphics.FromImage(image)) { var hdc = graphics.GetHdc(); viewObject.Draw(1, 0, IntPtr.Zero, tdevice, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); graphics.ReleaseHdc(hdc); } var format = Path.GetExtension(path)?.ToLower() == ".jpg" ? ImageFormat.Jpeg : ImageFormat.Png; image.Save(path, format); } }
/// <summary> /// スクリーンショットを保存します。 /// </summary> /// <param name="path">保存先。</param> /// <param name="format">画像のフォーマット。</param> private void SaveScreenShot(string path, System.Drawing.Imaging.ImageFormat format) { var wb = Browser; if (!IsKanColleLoaded) { AddLog(3, string.Format(Resources.NoScreenshotUnloaded)); return; } try { IViewObject viewobj = null; //int width = 0, height = 0; if (wb.Document.Url.AbsolutePath.Contains(".swf")) { viewobj = wb.Document.GetElementsByTagName("embed")[0].DomElement as IViewObject; if (viewobj == null) { throw new InvalidOperationException(Resources.CouldntLoadEmbed); } //width = ( (HTMLEmbed)viewobj ).clientWidth; //height = ( (HTMLEmbed)viewobj ).clientHeight; } else { var swf = getFrameElementById(wb.Document, "externalswf"); if (swf == null) { throw new InvalidOperationException(Resources.CouldntFindSWF); } Func <dynamic, bool> isvalid = target => { if (target == null) { return(false); } viewobj = target as IViewObject; if (viewobj == null) { return(false); } //if ( !int.TryParse( target.width, out width ) ) return false; //if ( !int.TryParse( target.height, out height ) ) return false; return(true); }; if (!isvalid(swf.DomElement as HTMLEmbed) && !isvalid(swf.DomElement as HTMLObjectElement)) { throw new InvalidOperationException(Resources.CouldntFindSWF); } } if (viewobj != null) { var rect = new RECT { left = 0, top = 0, width = KanColleSize.Width, height = KanColleSize.Height }; using (var image = new Bitmap(rect.width, rect.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { var device = new DVTARGETDEVICE { tdSize = 0 }; using (var g = Graphics.FromImage(image)) { var hdc = g.GetHdc(); viewobj.Draw(1, 0, IntPtr.Zero, device, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); g.ReleaseHdc(hdc); } image.Save(path, format); } } AddLog(2, string.Format(Resources.ScreenshotSaved, path)); } catch (Exception ex) { BrowserHost.AsyncRemoteRun(() => BrowserHost.Proxy.SendErrorReport(ex.ToString(), Resources.ScreenshotError)); } }
/// <summary> /// スクリーンショットを保存します。 /// </summary> /// <param name="path">保存先。</param> /// <param name="format">画像のフォーマット。</param> private void SaveScreenShot(string path, System.Drawing.Imaging.ImageFormat format) { var wb = Browser; if (!IsKanColleLoaded) { AddLog(3, string.Format("艦これが読み込まれていないため、スクリーンショットを撮ることはできません。")); return; } try { var document = wb.Document.DomDocument as HTMLDocument; if (document == null) { throw new InvalidOperationException("Document が取得できませんでした。"); } IViewObject viewobj = null; int width = 0, height = 0; if (document.url.Contains(".swf?")) { viewobj = document.getElementsByTagName("embed").item(0, 0) as IViewObject; if (viewobj == null) { throw new InvalidOperationException("embed 要素の取得に失敗しました。"); } width = ((HTMLEmbed)viewobj).clientWidth; height = ((HTMLEmbed)viewobj).clientHeight; } else { var gameFrame = document.getElementById("game_frame").document as HTMLDocument; if (gameFrame == null) { throw new InvalidOperationException("game_frame 要素の取得に失敗しました。"); } bool foundflag = false; for (int i = 0; i < document.frames.length; i++) { var provider = document.frames.item(i) as IServiceProvider; if (provider == null) { continue; } object ppvobj; provider.QueryService(typeof(SHDocVw.IWebBrowserApp).GUID, typeof(SHDocVw.IWebBrowser2).GUID, out ppvobj); var _wb = ppvobj as SHDocVw.IWebBrowser2; if (_wb == null) { continue; } var iframe = _wb.Document as HTMLDocument; if (iframe == null) { continue; } var swf = iframe.getElementById("externalswf"); if (swf == null) { continue; } Func <dynamic, bool> isvalid = target => { if (target == null) { return(false); } viewobj = target as IViewObject; if (viewobj == null) { return(false); } if (!int.TryParse(target.width, out width)) { return(false); } if (!int.TryParse(target.height, out height)) { return(false); } return(true); }; if (!isvalid(swf as HTMLEmbed) && !isvalid(swf as HTMLObjectElement)) { continue; } foundflag = true; break; } if (!foundflag) { throw new InvalidOperationException("対象の swf が見つかりませんでした。"); } } if (viewobj != null) { using (var image = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { var rect = new RECT { left = 0, top = 0, width = width, height = height }; var device = new DVTARGETDEVICE { tdSize = 0 }; using (var g = Graphics.FromImage(image)) { var hdc = g.GetHdc(); viewobj.Draw(1, 0, IntPtr.Zero, device, IntPtr.Zero, hdc, rect, null, IntPtr.Zero, IntPtr.Zero); g.ReleaseHdc(hdc); } image.Save(path, format); } } AddLog(2, string.Format("スクリーンショットを {0} に保存しました。", path)); } catch (Exception ex) { BrowserHost.AsyncRemoteRun(() => BrowserHost.Proxy.SendErrorReport(ex.ToString(), "スクリーンショットの保存時にエラーが発生しました。")); } }