48

内部で使用するための専用のクローラーとパーサーを作成しています。全体で使用されている色を確認するために、Webページのスクリーンショットを撮る機能が必要です。プログラムは約10個のWebアドレスを取り込み、それらをビットマップイメージとして保存します。

そこから、画像内で最も使用されている5つの色のリストを作成するために、LockBitsを使用する予定です。私の知る限り、これはWebページ内で使用される色を取得する最も簡単な方法ですが、より簡単な方法がある場合は、提案を取り入れてください。

とにかく、値札が表示されるまで、 ACA WebThumbActiveXControlを使用するつもりでした。また、C#は数か月しか使用していないので、かなり新しいです。配色を抽出するためにWebページのスクリーンショットを撮るという私の問題の解決策はありますか?

4

7 に答える 7

31

手っ取り早い方法は、WinForms WebBrowserコントロールを使用してビットマップに描画することです。基本的に非同期のプログラミング パターンを使用しながらSTAThreadコントロールをホストすることの影響を認識する必要があるため、スタンドアロン コンソール アプリでこれを行うのは少し注意が必要です。しかし、これは Web ページを 800x600 BMP ファイルにキャプチャする実際の概念実証です。

namespace WebBrowserScreenshotSample
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Threading;
    using System.Windows.Forms;

    class Program
    {
        [STAThread]
        static void Main()
        {
            int width = 800;
            int height = 600;

            using (WebBrowser browser = new WebBrowser())
            {
                browser.Width = width;
                browser.Height = height;
                browser.ScrollBarsEnabled = true;

                // This will be called when the page finishes loading
                browser.DocumentCompleted += Program.OnDocumentCompleted;

                browser.Navigate("https://stackoverflow.com/");

                // This prevents the application from exiting until
                // Application.Exit is called
                Application.Run();
            }
        }

        static void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Now that the page is loaded, save it to a bitmap
            WebBrowser browser = (WebBrowser)sender;

            using (Graphics graphics = browser.CreateGraphics())
            using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
            {
                Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                browser.DrawToBitmap(bitmap, bounds);
                bitmap.Save("screenshot.bmp", ImageFormat.Bmp);
            }

            // Instruct the application to exit
            Application.Exit();
        }
    }
}

System.Drawingこれをコンパイルするには、新しいコンソール アプリケーションを作成し、およびのアセンブリ参照を必ず追加してSystem.Windows.Formsください。

更新:ハッキーなポーリングの WaitOne/DoEvents パターンを使用する必要がないように、コードを書き直しました。このコードは、次のベスト プラクティスに近いはずです。

更新 2: Windows フォーム アプリケーションでこれを使用することを示します。その場合は、コントロールを動的に作成することを忘れてくださいWebBrowser。あなたが望むのはWebBrowser、フォーム上に a の非表示 (Visible=false) インスタンスを作成し、上記と同じ方法で使用することです。webAddressTextBox次に、テキスト ボックス ( )、ボタン ( generateScreenshotButton)、非表示のブラウザ ( )を含むフォームのユーザー コード部分を示す別のサンプルを示しますwebBrowser。これに取り組んでいるときに、以前は処理しなかった特異性を発見しました。DocumentCompleted イベントは、ページの性質に応じて、実際には複数回発生する可能性があります。このサンプルは一般的に機能するはずであり、必要に応じて拡張できます。

namespace WebBrowserScreenshotFormsSample
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Windows.Forms;

    public partial class MainForm : Form
    {
        public MainForm()
        {
            this.InitializeComponent();

            // Register for this event; we'll save the screenshot when it fires
            this.webBrowser.DocumentCompleted += 
                new WebBrowserDocumentCompletedEventHandler(this.OnDocumentCompleted);
        }

        private void OnClickGenerateScreenshot(object sender, EventArgs e)
        {
            // Disable button to prevent multiple concurrent operations
            this.generateScreenshotButton.Enabled = false;

            string webAddressString = this.webAddressTextBox.Text;

            Uri webAddress;
            if (Uri.TryCreate(webAddressString, UriKind.Absolute, out webAddress))
            {
                this.webBrowser.Navigate(webAddress);
            }
            else
            {
                MessageBox.Show(
                    "Please enter a valid URI.",
                    "WebBrowser Screenshot Forms Sample",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

                // Re-enable button on error before returning
                this.generateScreenshotButton.Enabled = true;
            }
        }

        private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // This event can be raised multiple times depending on how much of the
            // document has loaded, if there are multiple frames, etc.
            // We only want the final page result, so we do the following check:
            if (this.webBrowser.ReadyState == WebBrowserReadyState.Complete &&
                e.Url == this.webBrowser.Url)
            {
                // Generate the file name here
                string screenshotFileName = Path.GetFullPath(
                    "screenshot_" + DateTime.Now.Ticks + ".png");

                this.SaveScreenshot(screenshotFileName);
                MessageBox.Show(
                    "Screenshot saved to '" + screenshotFileName + "'.",
                    "WebBrowser Screenshot Forms Sample",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);

                // Re-enable button before returning
                this.generateScreenshotButton.Enabled = true;
            }
        }

        private void SaveScreenshot(string fileName)
        {
            int width = this.webBrowser.Width;
            int height = this.webBrowser.Height;
            using (Graphics graphics = this.webBrowser.CreateGraphics())
            using (Bitmap bitmap = new Bitmap(width, height, graphics))
            {
                Rectangle bounds = new Rectangle(0, 0, width, height);
                this.webBrowser.DrawToBitmap(bitmap, bounds);
                bitmap.Save(fileName, ImageFormat.Png);
            }
        }
    }
}
于 2009-12-30T19:26:02.037 に答える
30

https://screenshotlayer.com/documentationは最近見つけた唯一の無料サービスです...

イメージのバイナリをダウンロードするには、HttpWebRequest を使用する必要があります。詳細については、上記の URL を参照してください。

HttpWebRequest request = HttpWebRequest.Create("https://[url]") as HttpWebRequest;
Bitmap bitmap;
using (Stream stream = request.GetResponse().GetResponseStream())
{
    bitmap = new Bitmap(stream);
}
// now that you have a bitmap, you can do what you need to do...
于 2010-03-22T22:41:21.660 に答える
19

コマンドラインから任意の JavaScript を実行できる優れた Webkit ベースのブラウザー PhantomJS があります。

http://phantomjs.org/download.htmlからインストールし 、コマンド ラインから次のサンプル スクリプトを実行します。

./phantomjs ../examples/rasterize.js http://www.panoramio.com/photo/76188108 test.jpg

特定のページのスクリーンショットを JPEG ファイルで作成します。このアプローチの利点は、外部プロバイダーに依存せず、大量のスクリーンショットを簡単に自動化できることです。

于 2012-07-31T08:38:41.887 に答える
4

私は WebBrowser を使用しましたが、特に JavaScript のレンダリングが完了するのを待つ必要がある場合は、完全に機能しません。いくつかの Api を試したところ、 Seleniumが見つかりました。Seleniumで最も重要なことは、STAThread を必要とせず、単純なコンソール アプリやサービスで実行できることです。

試してみる :

class Program
{
    static void Main()
    {
        var driver = new FirefoxDriver();

        driver.Navigate()
            .GoToUrl("http://stackoverflow.com/");

        driver.GetScreenshot()
            .SaveAsFile("stackoverflow.jpg", ImageFormat.Jpeg);

        driver.Quit();
    }
}
于 2015-06-15T08:56:48.870 に答える
1

QTjambihttp ://qt.nokia.com/doc/qtjambi-4.4/html/com/trolltech/qt/qtjambi-index.htmlもご覧ください。

彼らはブラウザ用の素晴らしいウェブキットベースのJava実装を持っており、次のようにsthを実行するだけでスクリーンショットを作成できます。

    QPixmap pixmap;
    pixmap = QPixmap.grabWidget(browser);

    pixmap.save(writeTo, "png");

サンプルを見てください-彼らは素晴らしいウェブブラウザのデモを持っています。

于 2010-04-13T16:00:26.707 に答える
1

これをチェックしてください。これはあなたが望んでいたことをしているようで、技術的には、Web ブラウザー コントロールを介して非常によく似た方法で問題に取り組みます。さまざまなパラメーターが渡され、適切なエラー処理が組み込まれているようです。唯一の欠点は、生成するのは外部プロセス (exe) であり、後で読み取る物理ファイルを作成することです。あなたの説明から、あなたはWebサービスも考慮しているので、それは問題ではないと思います.

それらの複数を同時に処理する方法に関する最新のコメントを解決するには、これは完璧です。一度に 3、4、5、またはそれ以上のプロセスを並行して生成したり、別のキャプチャ プロセスが発生している間にカラー ビットの分析をスレッドとして実行したりできます。

画像処理については、最近Emguに出くわしました 、自分で使用したことはありませんが、魅力的です。高速であると主張しており、ピクセルの色の読み取りを含むグラフィック分析を多数サポートしています。手元にグラフィック処理プロジェクトがある場合は、これを試してみます。

于 2010-03-24T20:57:31.440 に答える