Skip to content

Instantly share code, notes, and snippets.

@exelix11
Created August 16, 2018 11:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save exelix11/a194d78d0fd0b7a88a800093af643d88 to your computer and use it in GitHub Desktop.
Save exelix11/a194d78d0fd0b7a88a800093af643d88 to your computer and use it in GitHub Desktop.
C# android screenshot in memory with ADB
//Rquired to run in C# Interactive (csi screenshot.csx)
#r "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll" //depends on your pc
using System.Windows.Forms;
using System.IO;
static void screenshot()
{
var process = new System.Diagnostics.Process();
var start = new System.Diagnostics.ProcessStartInfo()
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = @"adb.exe",
Arguments = "shell screencap -p"
};
process.StartInfo = start;
process.Start();
var stream = process.StandardOutput.BaseStream;
List<byte> data = new List<byte>(1024);
int read = 0;
bool isCR = false;
do
{
byte[] buf = new byte[1024];
read = stream.Read(buf, 0, buf.Length);
for (int i = 0; i < read; i++) //convert CRLF to LF
{
if (isCR && buf[i] == 0x0A)
{
isCR = false;
data.RemoveAt(data.Count - 1);
data.Add(buf[i]);
continue;
}
isCR = buf[i] == 0x0D;
data.Add(buf[i]);
}
}
while (read > 0);
if (data.Count == 0)
{
Console.WriteLine("fail");
return;
}
Form f = new Form() { Text = "screenshot" };
PictureBox pic = new PictureBox()
{
Image = new System.Drawing.Bitmap(new MemoryStream(data.ToArray())),
SizeMode = PictureBoxSizeMode.Zoom,
Dock = DockStyle.Fill
};
MenuStrip m = new MenuStrip() { Dock = DockStyle.Top };
m.Items.Add(new ToolStripMenuItem() { Text = "Save" });
m.Items[0].Click += delegate (object sender, EventArgs e)
{
var opn = new OpenFileDialog() { Filter = "png image|*.png" };
if (opn.ShowDialog() != DialogResult.OK)
return;
pic.Image.Save(opn.FileName);
};
f.Controls.Add(m);
f.Controls.Add(pic);
f.ShowDialog();
}
screenshot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment