How can I programmatically create a screen shot of a given Web site?

那年仲夏 提交于 2019-12-18 11:11:03

问题


I want to be able to create a screen shot of a given Web site, but the Web site may be larger than can be viewed on the screen. Is there a way I can do this?

Goal is to do this with .NET in C# in a WinForms application.


回答1:


There are a few tools.

The thing is, you need to render it in some given program, and take a snapshot of it. I don't know about .NET but here are some tools to look at.

  • KHTML2PNG
  • imagegrabwindow() (Windows PHP Only)
  • Create screenshots of a web page using Python and QtWebKit
  • Website Thumbnails Service
  • Taking automated webpage screenshots with embedded Mozilla



回答2:


I just found out about the website browsershots.org which generates screenshots for a whole bunch of different browsers. To a certain degree you can even specify the resolution.




回答3:


I wrote a program in VB.NET that did what you specified, except for the screen size issue.

I embedded a web control(look at the very bottom of all controls) onto my form, and tweaked it's settings(Hide scroll). I used a timer to wait on dynamic content, and then I used "copyFromScreen" to get the image.

My program had dynamic dimensions(settable via command line). I found that if I made my program larger than the screen, the image would just return black pixels for the off screen area. I did not research farther since my job was complete at that time.

Hope that gives you a good start. Sorry for any wrong wordings. I log onto windows to develop only once every couple of months.




回答4:


Doing at as a screen shot is likely to get ugly. It's easy enough to capture the entire content of the page with wget, but the image means capturing the rendering.

Here's some tools that purport to do it.




回答5:


You can render it on WebBrowser control and then take snapshot if page size bigger than screen size you have to scroll control take one or more snapshots and then merge all pictures :)




回答6:


This is the code for creating screenshot programatically:


using System.Drawing.Imaging;


int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);




回答7:


Java ScreenShots of WebSite

Combine Screens together for Final Entire WebPage Screenshot.

public static void main(String[] args) throws FileNotFoundException, IOException {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        ChromeDriver browser = new ChromeDriver();  
        WebDriver driver = browser;
        driver.get("https://news.google.co.in/");
        driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);

        JavascriptExecutor jse = (JavascriptExecutor) driver;
        Long clientHeight = (Long) jse.executeScript("return document.documentElement.clientHeight");
        Long scrollHeight = (Long) jse.executeScript("return document.documentElement.scrollHeight");
        int screens = 0, xAxis = 0, yAxis = clientHeight.intValue();
        String screenNames = "D:\\Screenshots\\Yash";
        for (screens = 0; ; screens++) {
            if (scrollHeight.intValue() - xAxis < clientHeight) {
                File crop = new File(screenNames + screens+".jpg");
                FileUtils.copyFile(browser.getScreenshotAs(OutputType.FILE), crop);                 

                BufferedImage image = ImageIO.read(new FileInputStream(crop));            
                int y_Axixs = scrollHeight.intValue() - xAxis;
                BufferedImage croppedImage = image.getSubimage(0, image.getHeight()-y_Axixs, image.getWidth(), y_Axixs);
                ImageIO.write(croppedImage, "jpg", crop);               
                break;
            }               

FileUtils.copyFile(browser.getScreenshotAs(OutputType.FILE), new File(screenNames + screens+".jpg")); 
 jse.executeScript("window.scrollBy("+ xAxis +", "+yAxis+")");

                jse.executeScript("var elems = window.document.getElementsByTagName('*');"                              
                        + "     for(i = 0; i < elems.length; i++) { "
                        + "         var elemStyle = window.getComputedStyle(elems[i], null);"
                        + "         if(elemStyle.getPropertyValue('position') == 'fixed' && elems[i].innerHTML.length != 0 ){"                                                                  
                        + "             elems[i].parentNode.removeChild(elems[i]); "                                
                        + "}}");    // Sticky Content Removes
                xAxis += yAxis;
        }
        driver.quit();
    }


来源:https://stackoverflow.com/questions/762162/how-can-i-programmatically-create-a-screen-shot-of-a-given-web-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!