How can I programmatically create a screen shot of

2019-02-02 02:12发布

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.

7条回答
Ridiculous、
2楼-- · 2019-02-02 02:53

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.

查看更多
做自己的国王
3楼-- · 2019-02-02 02:54

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();
    }
查看更多
聊天终结者
4楼-- · 2019-02-02 03:02

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

查看更多
乱世女痞
5楼-- · 2019-02-02 03:09

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.

查看更多
做个烂人
6楼-- · 2019-02-02 03:09

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.

查看更多
beautiful°
7楼-- · 2019-02-02 03:10

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.

查看更多
登录 后发表回答