cscの作法 その171

# 概要
cscの作法、調べてみた。
ChromeDevToolsProtocol、叩いてみた。
スクリーンショット撮ってみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/4daca975-f97f-d8a9-a758-3191130fafcb.png)


# サンプルコード

```
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Diagnostics;
using WebSocketSharp;
using WebSocketSharp.Net;
using LitJson;
using System.Net;
using System.Text;
using System.Web;
using System.IO;

public class Form1 : Form {
	private WebSocket client;
	Button button0,
		button1,
		button2,
		button3,
		button4;
	string webSocketDebuggerUrl;
	public Form1() {
		this.ClientSize = new Size(400, 400);
		this.Text = "cdp";
		button0 = new Button();
		button0.Location = new Point(10, 10);
		button0.Size = new Size(80, 30);
		button0.Text = "open";
		button1 = new Button();
		button1.Location = new Point(110, 10);
		button1.Size = new Size(80, 30);
		button1.Text = "start";
		button2 = new Button();
		button2.Location = new Point(210, 10);
		button2.Size = new Size(80, 30);
		button2.Text = "shot";
		button3 = new Button();
		button3.Location = new Point(10, 60);
		button3.Size = new Size(80, 30);
		button3.Text = "";
		button4 = new Button();
		button4.Location = new Point(110, 60);
		button4.Size = new Size(80, 30);
		button4.Text = "";
		Controls.Add(button0);
		Controls.Add(button1);
		Controls.Add(button2);
		Controls.Add(button3);
		Controls.Add(button4);
		button0.Click += Button0_Click;
		button1.Click += Button1_Click;
		button2.Click += Button2_Click;
		button3.Click += Button3_Click;
		button4.Click += Button4_Click;
	}
	private void Button0_Click(object sender, EventArgs e) {
		using (var process = new Process()) 
		{
			process.StartInfo = new ProcessStartInfo {
				FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
				Arguments = @"--remote-debugging-Port=9222",
				CreateNoWindow = true,
				UseShellExecute = false,
			};
			process.Start();
		}
	}
	private void Button1_Click(object sender, EventArgs e) {
		WebRequest request = WebRequest.Create("http://localhost:9222/json");
		WebResponse response = request.GetResponse();
		using (Stream dataStream = response.GetResponseStream())
		{
			StreamReader reader = new StreamReader(dataStream);
			string responseFromServer = reader.ReadToEnd();
			JsonData jsonData = JsonMapper.ToObject(responseFromServer);
			webSocketDebuggerUrl = (string) jsonData[0]["webSocketDebuggerUrl"];
			client = new WebSocket(webSocketDebuggerUrl);
			client.OnOpen += (ss, ee) => {
				Console.WriteLine(string.Format("Connected : to {0} successfully ", webSocketDebuggerUrl));
			};
			client.OnError += (ss, ee) => {
				Console.WriteLine("Error : " + ee.Message);
			};
			client.OnMessage += (ss, ee) => {
				Console.WriteLine("Echo : " + ee.Data);
				JsonData pngData = JsonMapper.ToObject(ee.Data);
				Console.WriteLine(pngData["id"]);
				byte[] bt = Convert.FromBase64String((string) pngData["result"]["data"]);
				string path = @"C:\Users\user\shot.png";
				using (var fs = new FileStream(path, FileMode.Create))
				{
					fs.Write(bt, 0, bt.Length);
				}
			};
			client.OnClose += (ss, ee) => {
				Console.WriteLine(string.Format("Disconnected : with {0} ", webSocketDebuggerUrl));
			};
			client.Connect();
		}
		response.Close();
	}
	private void Button2_Click(object sender, EventArgs e) {
		string json = "{\"id\": 2, \"method\": \"Page.captureScreenshot\", \"params\": {\"format\": \"png\"}}";
		Console.WriteLine(json); 
		if (!string.IsNullOrEmpty(json))
			client.Send(json);
	}
	private void Button3_Click(object sender, EventArgs e) {
	}
	private void Button4_Click(object sender, EventArgs e) {
	}
	[STAThread]
	public static void Main() {
		Application.Run(new Form1());
	}
}





```


以上。