Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking screenshot of a webpage programmatically [closed]

How do take a sceenshot of a webpage programmatically given the URL as input?

And here is what I have till now:

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

But this is not working. It is only giving me a white blank picture. What am I missing here?

Is there any other way I could get the screenshot of a web page programmatically?

like image 814
Manish Avatar asked Feb 23 '10 08:02

Manish


People also ask

Can a website disable screenshot?

No, it's not. That screenshot function is part of the mobile phone (or PC for that matter) and nothing you can send via a web page will stop that.

Can JavaScript take screenshot?

A screenshot of any element in JavaScript can be taken using the html2canvas library. This library can be downloaded from its official website. The below steps show the method to take a screenshot of a <div> element using JavaScript.

How can I screen shot an entire webpage?

Click the three-dot icon from the top-right corner and select Run command. Also, you can press Ctrl+Shift+P on Windows or Command+Shift+P on Mac. Type screenshot into the search box. Select Capture full-size screenshot.

How can I take a screenshot of an entire website in one click?

One-click Screenshot. Take screenshots of entire websites. Alt+Shift+S is the default shortcut (Option+Shift+S on Mac).


2 Answers

I searched and searched and searched and found it Webpage thumbnailer (a The Code Project article).

like image 86
Manish Avatar answered Sep 21 '22 18:09

Manish


Drawing the browser control to a bitmap is somewhat unreliable. I think it would be better to just screenscrape your window.

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), 
        bitmap.Size);
        bitmap.Save(filename);
}
like image 24
Gabe Avatar answered Sep 19 '22 18:09

Gabe