webkit,html保存png

var url = args[0];//本地html文件地址或者html的url地址
            var destPath = args[1];//E:\code\1 文件夹,
            var imageName = args[2];//图片名称
            AsyncContext.Run(async delegate
            {

                try
                {
                    if (!Directory.Exists(destPath))
                    {
                        Directory.CreateDirectory(destPath);
                    }
                    var cefSharplog = Path.Combine(Environment.CurrentDirectory, "CefSharp\\Cache");
                    WriteLog(cefSharplog, "");
                    var settings = new CefSettings()
                    {
                        //默认情况下,CefSharp将使用内存缓存,您需要指定缓存文件夹来持久化数据
                        CachePath = cefSharplog
                    };

                    //执行依赖项检查以确保所有相关资源都在我们的输出目录中。
                    var success = await Cef.InitializeAsync(settings, performDependencyCheck: true, browserProcessHandler: null);

                    if (!success)
                    {
                        WriteLog("Unable to initialize CEF, check the log file", "");
                        throw new Exception("Unable to initialize CEF, check the log file.");
                    }

                    // 创建ChromiumWebBrowser实例
                    using (var browser = new ChromiumWebBrowser(url))
                    {
                        var initialLoadResponse = await browser.WaitForInitialLoadAsync();

                        if (!initialLoadResponse.Success)
                        {
                            throw new Exception(string.Format("Page load failed with ErrorCode:{0}, HttpStatusCode:{1}", initialLoadResponse.ErrorCode, initialLoadResponse.HttpStatusCode));
                        }
                        //等待浏览器渲染
                        await Task.Delay(500);
                        // 截屏
                        var bitmapAsByteArray = await browser.CaptureScreenshotAsync();
                        if (!Directory.Exists(destPath))
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        // 要保存屏幕截图的文件路径
                        var screenshotPath = Path.Combine(destPath, imageName);

                        File.WriteAllBytes(screenshotPath, bitmapAsByteArray);

                         告诉Windows启动保存的图像
                        //Process.Start(new ProcessStartInfo(screenshotPath)
                        //{
                        //    // .net core中默认情况下UseShellExecute为false
                        //    UseShellExecute = true
                        //});
                    }
                    WriteLog("图片生成成功", Path.Combine(destPath, imageName));
                    Cef.Shutdown();
                }
                catch (Exception e)
                {
                    WriteLog(e.Message, "");
                    Cef.Shutdown();
                }
            });


 public static class AsyncContext
    {
        public static void Run(Func<Task> func)
        {
            var prevCtx = SynchronizationContext.Current;

            try
            {
                var syncCtx = new SingleThreadSynchronizationContext();

                SynchronizationContext.SetSynchronizationContext(syncCtx);

                var t = func();

                t.ContinueWith(delegate
                {
                    syncCtx.Complete();
                }, TaskScheduler.Default);

                syncCtx.RunOnCurrentThread();

                t.GetAwaiter().GetResult();
            }
            finally
            {

                SynchronizationContext.SetSynchronizationContext(prevCtx);
            }
        }
    }
    public sealed class SingleThreadSynchronizationContext : SynchronizationContext
    {
        private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> queue =
            new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();

        public override void Post(SendOrPostCallback d, object state)
        {
            queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
        }

        public void RunOnCurrentThread()
        {
            while (queue.TryTake(out var workItem, Timeout.Infinite))
            {
                workItem.Key(workItem.Value);
            }
        }

        public void Complete()
        {
            queue.CompleteAdding();
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值