質問

どのようにプログラムでURLを入力として与えられたWebページのsceenshotを取るのですか?

ここで

とは、私が今まで持っているものです。

// The size of the browser window when we want to take the screenshot (and the size of the resulting bitmap)
Bitmap bitmap = new Bitmap(1024, 768);
Rectangle bitmapRect = new Rectangle(0, 0, 1024, 768);
// This is a method of the WebBrowser control, and the most important part
webBrowser1.DrawToBitmap(bitmap, bitmapRect);

// Generate a thumbnail of the screenshot (optional)
System.Drawing.Image origImage = bitmap;
System.Drawing.Image origThumbnail = new Bitmap(120, 90, origImage.PixelFormat);

Graphics oGraphic = Graphics.FromImage(origThumbnail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle(0, 0, 120, 90);
oGraphic.DrawImage(origImage, oRectangle);

// Save the file in PNG format
origThumbnail.Save(@"d:\Screenshot.png", ImageFormat.Png);
origImage.Dispose();

しかし、これは動作しません。それだけで私に白抜きの絵を与えています。私はここで何をしないのですか?

私は、Webページのスクリーンショットを得ることができる他の方法はありますプログラムで?

役に立ちましたか?

解決

私が検索し、検索し、検索して、ウェブページthumbnailerの の(A コードプロジェクトの記事)。

他のヒント

ビットマップにブラウザコントロールを描画すると、やや信頼性に欠けます。私はそれだけで、あなたのウィンドウをスクリーンスする方が良いと思います。

using (Bitmap bitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height, PixelFormat.Format24bppRgb))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
    graphics.CopyFromScreen(
        PointToScreen(webBrowser1.Location),
        new Point(0, 0), 
        bitmapSize);
        bitmap.Save(filename);
}

あなたのネイティブPrintWindow関数を呼び出す試すことができます。

またBitBlt()からgdi32.dllを起動/ Pを試みることができます。このコードを試してみてください。

Graphics mygraphics = webBrowser1.CreateGraphics();
Size s = new Size(1024, 768);
Bitmap memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
// P/Invoke call here
BitBlt(dc2, 0, 0, webBrowser1.ClientRectangle.Width, webBrowser1.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
memoryImage.Save(filename);

P /呼び出しは以下のようになります:

[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top