php结合c#实现后台网页(无需打开浏览器)截图

php结合c#实现后台网页(无需打开浏览器)截图

php结合c#实现后台网页(无需打开浏览器)截图

项目中有时需要根据url来获取这个页面的截图,怎么办呢,php还没有办法截图,php暂时还没办法后台对网页截图,但是c#可以在命令行中调用cefsharp进行网页截图,然后返回给php进行调用

c#主要代码如下,新建一个console项目,引入cefsharp,这里有教程怎么引入cefsharp   http://blog.bfw.wiki/user6/15572101669805510054.html

主要用到的方法就是ScreenshotAsync

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using CefSharp.OffScreen;
using System.Threading.Tasks;

namespace CefSharp.bfw.OffScreen
{
    public class Program
    {
        private static ChromiumWebBrowser browser;

        public static void Main(string[] args)
        {
            const string testUrl = "https://www.baidu.com/";

            Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", testUrl);
            Console.WriteLine("You may see Chromium debugging output, please wait...");
            Console.WriteLine();

            //Monitor parent process exit and close subprocesses if parent process exits first
            //This will at some point in the future becomes the default
            CefSharpSettings.SubprocessExitIfParentProcessClosed = true;

            var settings = new CefSettings()
            {
                //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
                CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
            };

            //Perform dependency check to make sure all relevant resources are in our output directory.
            Cef.Initialize(settings, performDependencyCheck: tr...

点击查看剩余70%

{{collectdata}}

网友评论0