Пример #1
0
        /// <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(), "スクリーンショットの保存時にエラーが発生しました。"));
            }
        }