2010-02-23 6 views
15

Wie kann man einen Bildschirmausdruck einer Webseite programmatisch mit der URL als Eingabe erstellen?Screenshot einer Webseite programmgesteuert erstellen

Und hier ist, was ich habe bis jetzt:

// 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(); 

Aber das funktioniert nicht. Es gibt mir nur ein weißes, leeres Bild. Was fehlt mir hier?

Gibt es eine andere Möglichkeit, den Screenshot einer Webseite programmgesteuert zu erhalten?

+0

Diese Frage wurde erst gestern gestellt, obwohl sie sich hauptsächlich an Perl richtete. Vielleicht würden dir einige der Antworten dort helfen, obwohl sie dir natürlich eine andere Richtung weisen würden. Hier ist der [link] (http://stackoverflow.com/questions/2312852/how-can-i-take-screenshots-with-perl). – lundmark

Antwort

2

Sie können versuchen, die native PrintWindow-Funktion aufzurufen.

+1

Kannst du dazu ein bisschen mehr erklären? Bitte beachten Sie, dass ich nur die URL der Webseite als Eingabe habe. – Manish

3

Das Zeichnen des Browser-Steuerelements zu einer Bitmap ist etwas unzuverlässig. Ich denke, es wäre besser, nur dein Fenster zu screenen.

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); 
} 
+3

Dieser Ansatz wird in der Konsolen-App nicht funktionieren, oder? –

0

Sie könnten auch versuchen, P/BitBlt() von gdi32.dll aufrufen. Versuchen Sie diesen Code:

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); 

Die P/Invoke wäre:

[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);