【C#】NET截屏网页,生成网页快照开发——IECapt、CutyCapt


软件介绍

IECapt、CutyCapt 生成网页快照

http://iecapt.sourceforge.net/
http://cutycapt.sourceforge.net/
### 操作代码 
1.以管理员身份 运行cmd.exe
  
切换到 IECapt.exe  所在目录
例如:
输入:D:
输入:cd  D:\AppData\
输入截图命令:
输入:CutyCapt  --url=https://tieba.baidu.com/p/5217647622 --out=D:/1.jpeg --silent
输入:iecapt  --url=https://tieba.baidu.com/p/5217647622 --out=D:/1.jpeg --silent
看下D:盘目录下是否存在
 

操作演示

代码示例

#region CutyCapt
//Open a command prompt and ask for help:

// % CutyCapt --help
// -----------------------------------------------------------------------------
// Usage: CutyCapt --url=http://www.example.org/ --out=localfile.png            
// -----------------------------------------------------------------------------
//  --help Print this help page and exit                
//  --url=< url > The URL to capture (http:...|file:...|...)   
//  --out=                   The target file (.png|pdf|ps|svg|jpeg|...)   
//  --out-format=               Like extension in --out, overrides heuristic 
//  --min-width=              Minimal width for the image (default: 800)   
//  --min-height=             Minimal height for the image (default: 600)  
//  --max-wait=                Don't wait more than (default: 90000, inf: 0)
//  --delay=                   After successful load, wait (default: 0)     
//  --user-style-path=       Location of user style sheet file, if any    
//  --user-style-string=      User style rules specified as text           
//  --header=:        request header; repeatable; some can't be set
//  --method= Specifies the request method(default: get)
//  --body - string =< string > Unencoded request body(default: none)       
//  --body - base64 =< base64 > Base64 - encoded request body(default: none)  
//  --app - name =< name > appName used in User - Agent;
//   default is none
//  --app - version =< version > appVers used in User - Agent;
//   default is none
//  --user - agent =< string > Override the User-Agent header Qt would set
//  --javascript =< on | off > JavaScript execution(default: on)
//  --java =< on | off > Java execution(default: unknown)
//  --plugins =< on | off > Plugin execution(default: unknown)
//  --private-browsing=    Private browsing(default: unknown)
//  --auto-load-images=    Automatic image loading(default: on)
//  --js-can-open-windows= Script can open windows? (default: unknown)  
//  --js-can-access-clipboard= Script clipboard privs(default: unknown)
//  --print-backgrounds=   Backgrounds in PDF/PS output(default: off)
//  --zoom-factor=          Page zoom factor(default: no zooming)
//  --zoom-text-only=      Whether to zoom only the text(default: off)
//  --http-proxy=             Address for HTTP proxy server(default: none)
// -----------------------------------------------------------------------------
//   is svg,ps,pdf,itext,html,rtree,png,jpeg,mng,tiff,gif,bmp,ppm,xbm,xpm    
// -----------------------------------------------------------------------------
// http://cutycapt.sf.net - (c) 2003-2013 Bjoern Hoehrmann - bjoern@hoehrmann.de 
#endregion
#region IECapt
// Open a command prompt and ask for help:
//C:\> IECapt --help
// -----------------------------------------------------------------------------
// Usage: IECapt --url=http://www.example.org/ --out=localfile.png
// -----------------------------------------------------------------------------
//  --help                      Print this help page and exit
//  --url=                 The URL to capture (http:...|file:...|...)
//  --out=                The target file (.png|bmp|jpeg|emf|...)
//  --min-width=           Minimal width for the image (default: 800)
//  --max-wait=             Don't wait more than (default: 90000, inf: 0)
//  --delay=                Wait after loading (e.g. for Flash; default: 0)
//  --silent                    Whether to surpress some dialogs
// -----------------------------------------------------------------------------
// http://iecapt.sf.net - (c) 2003-2008 Bjoern Hoehrmann - "/> 
#endregion





using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;

namespace Capt.Helper
{

  public class CaptHelper
  {
    /// 
    /// 执行截图操作
    /// 
    /// 网页链接,example:"https://www.baidu.com/" 
    /// 
    public static ReturnResult Execute(string url, IECaptOrCutyCapt type = IECaptOrCutyCapt.IECapt)
    {
      if (string.IsNullOrEmpty(url))
      {
        return new ReturnResult() { Msg = "url 为空" };
      }
      url = (url.IndexOf("http://", StringComparison.OrdinalIgnoreCase) > -1 ||
        url.IndexOf("https://", StringComparison.OrdinalIgnoreCase) > -1) ? url : "http://" + url;
      var path = AppDomain.CurrentDomain.BaseDirectory + "TempFiles\\Image";
      if (!Directory.Exists(path))
      {
        Directory.CreateDirectory(path);
      }
      string fileName = Guid.NewGuid().ToString("N") + ".png";
      string completePath = Path.Combine(path, fileName);
      var data = Execute(new CaptInfo() { Url = url, Out = completePath, CaptType = type });
      data.Data = completePath;
      return data;
    }
    /// 
    /// 执行截图操作
    /// 
    /// 
    /// 物理路径,
    /// 
    public static ReturnResult Execute(string url, string path,
      IECaptOrCutyCapt type = IECaptOrCutyCapt.IECapt)
    {
      if (string.IsNullOrEmpty(url))
      {
        return new ReturnResult() { Msg = "url 为空" };
      }
      url = (url.IndexOf("http://", StringComparison.OrdinalIgnoreCase) > -1 ||
        url.IndexOf("https://", StringComparison.OrdinalIgnoreCase) > -1) ? url : "http://" + url;

      if (!Directory.Exists(path))
        Directory.CreateDirectory(path);
      string fileName = Guid.NewGuid().ToString("N") + ".png";
      string completePath = Path.Combine(path, fileName);
      var data = Execute(new CaptInfo() { Url = url, Out = completePath, CaptType = type });
      data.Data = completePath;
      return data;
    }
    /// 
    /// 执行输出快照
    /// 
    /// CaptInfo
    /// 
    public static ReturnResult Execute(CaptInfo info)
    {
      string output = string.Empty;
      Stopwatch sw = Stopwatch.StartNew();
      string root = string.Empty;
      if (info.CaptType == IECaptOrCutyCapt.IECapt)
      {
        root = AppDomain.CurrentDomain.BaseDirectory + @"Lib\\IECapt";
        if (!File.Exists(root + "\\IECapt.exe"))
          throw new FileNotFoundException("IECapt.exe file can't be found .");
      }
      else
      {
        root = AppDomain.CurrentDomain.BaseDirectory + @"Lib\\CutyCapt";
        if (!File.Exists(root + "\\CutyCapt.exe"))
          throw new FileNotFoundException("IECapt.exe file can't be found .");
      }

      using (var process = new Process())
      {
        try
        {
          process.StartInfo.WorkingDirectory = root;
          process.StartInfo.FileName = "cmd.exe";
          process.StartInfo.UseShellExecute = false;
          process.StartInfo.RedirectStandardInput = true;
          process.StartInfo.RedirectStandardOutput = true;
          process.StartInfo.CreateNoWindow = true;

          //process.StartInfo.CreateNoWindow = false;
          process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
          process.Start();
          string value = string.Format(@"{0} --url={1} --out={2} --min-width={3} --max-wait={4} --delay={5} --silent",
            info.CaptType == IECaptOrCutyCapt.IECapt ? "iecapt" : "cutycapt", //输出方式
            info.Url,  //输入路径网站
            info.Out,   //输出
            info.Min_width,
            info.Max_wait,
            info.Delay);
          process.StandardInput.WriteLine(value);
          process.StandardInput.WriteLine("exit");
          process.WaitForExit(info.WaitForExitTime > 0 ? info.WaitForExitTime : 6000);
          output = process.StandardOutput.ReadToEnd();

        }
        catch (Exception ex)
        {
          return new ReturnResult()
          {
            Status = CaptStatus.ErrorException,
            Msg = "快照失败:" + ex.Message
          };
        }
        finally
        {
          sw.Stop();
          if (!process.HasExited)
          {
            process.Kill();
          }
          process.Close();
          process.Dispose();


        }
        if (System.IO.File.Exists(info.Out))
        {
          return new ReturnResult()
          {
            Status = CaptStatus.Success,
            QTime = sw.ElapsedMilliseconds,
            Msg = "快照生产成功:" + output
          };
        }
        else
        {
          return new ReturnResult()
          {
            Status = CaptStatus.ErrorNotOutFile,
            Msg = "快照失败,文件不存在"
          };
        }


      }
    }

  }
}

示例代码库

源码地址:参考点击:https://github.com/Sopcce/sop-dotnet-iecapt

Net