blob: 0b2c507631554cdd05b8f24cc58621327145dcee [file] [log] [blame] [raw]
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
namespace Rivoreo.ScreenShooter {
class SelectRectangleForm : Form {
public SelectRectangleForm() {
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
WindowState = FormWindowState.Maximized;
TopMost = true;
Cursor = Cursors.Cross;
DoubleBuffered = true;
//SetStyle(ControlStyles.Opaque, true);
//TransparencyKey = BackColor;
BackColor = Color.Black;
Opacity = 0.5;
ShowInTaskbar = false;
Location = SystemInformation.VirtualScreen.Location;
Size = SystemInformation.VirtualScreen.Size;
BackgroundImage = new Bitmap(Size.Width, Size.Height);
Graphics graphics = Graphics.FromImage(BackgroundImage);
graphics.CopyFromScreen(Location, new Point(), Size);
}
private Point initial_location;
private Rectangle selection;
/*
public new Point Location {
get { return selection.Location; }
}
public new Size Size {
get { return selection.Size; }
}
*/
public Bitmap Bitmap {
get {
Bitmap bitmap = (Bitmap)BackgroundImage;
return bitmap.Clone(selection, bitmap.PixelFormat);
}
}
protected override void OnPaint(PaintEventArgs a) {
using(Pen pen = new Pen(Color.White, 1)) {
a.Graphics.DrawRectangle(pen, selection);
}
}
protected override void OnMouseDown(MouseEventArgs a) {
if(a.Button != MouseButtons.Left) return;
initial_location = a.Location;
selection = new Rectangle(a.Location, new Size(0, 0));
}
protected override void OnMouseMove(MouseEventArgs a) {
if(a.Button != MouseButtons.Left) return;
if(a.X < initial_location.X) {
selection.X = a.X;
selection.Width = initial_location.X - a.X;
} else {
selection.Width = a.X - initial_location.X;
}
if(a.Y < initial_location.Y) {
selection.Y = a.Y;
selection.Height = initial_location.Y - a.Y;
} else {
selection.Height = a.Y - initial_location.Y;
}
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs a) {
Console.Error.WriteLine(selection);
DialogResult = selection.Width > 0 && selection.Height > 0 ?
DialogResult.OK : DialogResult.Abort;
Close();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keys) {
if(keys == Keys.Escape) {
DialogResult = DialogResult.Cancel;
Close();
}
return base.ProcessCmdKey(ref msg, keys);
}
}
enum ShootTarget {
FULL_SCREEN, ACTIVE_WINDOW, SELECT_RECTANGLE, SELECT_WINDOW, DYNAMIC_SELECT_RECTANGLE, DYNAMIC_SELECT_WINDOW
}
class MainForm : Form {
public MainForm(ShootTarget target) {
shoot_screen(target);
if(bitmap == null) Application.Exit();
SuspendLayout();
main_panel = new TableLayoutPanel();
main_panel.SuspendLayout();
main_panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
main_panel.ColumnCount = 4;
main_panel.RowCount = 5;
main_panel.ColumnStyles.Add(new ColumnStyle());
main_panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 90));
main_panel.ColumnStyles.Add(new ColumnStyle());
main_panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 90));
main_panel.BorderStyle = BorderStyle.FixedSingle;
//预览区域 = new Label();
预览区域 = new PictureBox();
//预览区域.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
预览区域.Dock = DockStyle.Fill;
预览区域.Location = new Point(20, 10);
//预览区域.Height = 110;
//预览区域.Width = 232;
//预览区域.Paint += PaintPreview;
//Graphics graphics = 预览区域.CreateGraphics();
预览区域.AutoSize = true;
预览区域.Image = bitmap;
预览区域.SizeMode = PictureBoxSizeMode.Zoom;
main_panel.Controls.Add(预览区域, 0, 0);
main_panel.SetColumnSpan(预览区域, 4);
//main_panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
main_panel.RowStyles.Add(new RowStyle(SizeType.Percent, 99));
file_name_label = new Label();
file_name_label.Anchor = AnchorStyles.Right;
file_name_label.AutoSize = true;
file_name_label.Text = "File name";
main_panel.Controls.Add(file_name_label, 0, 1);
file_name_text_box = new TextBox();
//file_name_text_box.AutoSize = true;
file_name_text_box.Dock = DockStyle.Fill;
DateTime dt = DateTime.Now;
file_name_text_box.Text =
"screenshot." + dt.ToString("yyyy-MM-dd.HH-mm-ss") + ".png";
file_name_text_box.TabIndex = 1;
main_panel.Controls.Add(file_name_text_box, 1, 1);
main_panel.SetColumnSpan(file_name_text_box, 3);
main_panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 32));
save_path_label = new Label();
save_path_label.Anchor = AnchorStyles.Right;
save_path_label.AutoSize = true;
save_path_label.Text = "Save to";
main_panel.Controls.Add(save_path_label, 0, 2);
save_path_text_box = new TextBox();
//save_path_text_box.AutoSize = true;
save_path_text_box.Dock = DockStyle.Fill;
save_path_text_box.Text = "/tmp/";
save_path_text_box.TabIndex = 2;
main_panel.Controls.Add(save_path_text_box, 1, 2);
main_panel.SetColumnSpan(save_path_text_box, 3);
main_panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 32));
delay_label = new Label();
delay_label.Anchor = AnchorStyles.Right;
delay_label.AutoSize = true;
delay_label.Text = "Delay";
main_panel.Controls.Add(delay_label, 0, 3);
delay_box = new NumericUpDown();
delay_box.AutoSize = true;
//delay_box. = 0;
delay_box.TabIndex = 3;
main_panel.Controls.Add(delay_box, 1, 3);
target_label = new Label();
target_label.Anchor = AnchorStyles.Right;
target_label.AutoSize = true;
target_label.Text = "Target";
main_panel.Controls.Add(target_label, 2, 3);
target_box = new ComboBox();
target_box.AutoSize = true;
target_box.DropDownStyle = ComboBoxStyle.DropDownList;
target_box.Items.AddRange(new Object[] {
"Full screen", "Active window", "Select region", "Select window"
});
target_box.SelectedIndex = 0;
target_box.TabIndex = 4;
main_panel.Controls.Add(target_box, 3, 3);
main_panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 32));
button_panel = new FlowLayoutPanel();
button_panel.SuspendLayout();
button_panel.AutoSize = true;
button_panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
button_panel.FlowDirection = FlowDirection.LeftToRight;
button_panel.WrapContents = false;
button_panel.BorderStyle = BorderStyle.FixedSingle;
left_button_panel = new FlowLayoutPanel();
left_button_panel.SuspendLayout();
left_button_panel.AutoSize = true;
left_button_panel.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
left_button_panel.Dock = DockStyle.Fill;
left_button_panel.FlowDirection = FlowDirection.LeftToRight;
left_button_panel.WrapContents = false;
重新截图按钮 = new Button();
重新截图按钮.Text = "&New";
重新截图按钮.Width = 64;
重新截图按钮.TabIndex = 6;
重新截图按钮.Click += 重新截图;
left_button_panel.Controls.Add(重新截图按钮);
退出按钮 = new Button();
//退出按钮.Anchor = AnchorStyles.Left | AnchorStyles.Right;
退出按钮.Text = "&Quit";
//退出按钮.Location = new Point(20, 124);
退出按钮.Width = 64;
退出按钮.TabIndex = 7;
退出按钮.Click += Quit;
left_button_panel.Controls.Add(退出按钮);
left_button_panel.ResumeLayout();
button_panel.Controls.Add(left_button_panel);
right_button_panel = new FlowLayoutPanel();
right_button_panel.SuspendLayout();
right_button_panel.AutoSize = true;
right_button_panel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
right_button_panel.Dock = DockStyle.Fill;
right_button_panel.FlowDirection = FlowDirection.LeftToRight;
right_button_panel.WrapContents = false;
复制按钮 = new Button();
复制按钮.Text = "&Copy";
复制按钮.Width = 64;
复制按钮.TabIndex = 5;
复制按钮.Click += Copy;
right_button_panel.Controls.Add(复制按钮);
保存按钮 = new Button();
保存按钮.Text = "&Save";
保存按钮.Width = 64;
//保存按钮.Location = new Point(50, 124);
保存按钮.TabIndex = 0;
保存按钮.Click += Quit;
right_button_panel.Controls.Add(保存按钮);
right_button_panel.ResumeLayout();
button_panel.Controls.Add(right_button_panel);
button_panel.Height = 32;
button_panel.ResumeLayout();
main_panel.Controls.Add(button_panel, 0, 4);
main_panel.SetColumnSpan(button_panel, 4);
main_panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 40));
main_panel.ResumeLayout();
Controls.Add(main_panel);
Text = "Screen Shooter";
ClientSize = new Size(240, 200);
if(target == ShootTarget.FULL_SCREEN) CenterToScreen();
AutoScaleMode = AutoScaleMode.Font;
AutoSize = true;
ResumeLayout();
main_panel.ClientSize = ClientSize - new Size(2, 2);
if(预览区域.Width > main_panel.Width - 8) {
预览区域.Width = main_panel.Width - 8;
}
save_path_text_box.Width = main_panel.Width - 32;
//bitmap.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
}
private Bitmap bitmap;
private TableLayoutPanel main_panel;
private FlowLayoutPanel button_panel;
private FlowLayoutPanel left_button_panel;
private FlowLayoutPanel right_button_panel;
private PictureBox 预览区域;
private Label file_name_label;
private TextBox file_name_text_box;
private Label save_path_label;
private TextBox save_path_text_box;
private Label delay_label;
internal NumericUpDown delay_box;
private Label target_label;
private ComboBox target_box;
private Button 重新截图按钮;
private Button 退出按钮;
private Button 复制按钮;
private Button 保存按钮;
private void shoot_screen(ShootTarget target) {
Point location;
Size size;
switch(target) {
case ShootTarget.FULL_SCREEN:
//location.X = 0;
//location.Y = 0;
location = SystemInformation.VirtualScreen.Location;
//size.Width = SystemInformation.VirtualScreen.Width;
//size.Height = SystemInformation.VirtualScreen.Height;
size = SystemInformation.VirtualScreen.Size;
break;
case ShootTarget.ACTIVE_WINDOW:
throw new NotImplementedException();
// break;
case ShootTarget.SELECT_RECTANGLE:
using(SelectRectangleForm f = new SelectRectangleForm()) {
if(f.ShowDialog() != DialogResult.OK) return;
//location = f.Location;
//size = f.Size;
bitmap = f.Bitmap;
}
return;
case ShootTarget.SELECT_WINDOW:
throw new NotImplementedException();
// break;
case ShootTarget.DYNAMIC_SELECT_RECTANGLE:
//select_rectangle(out location, out size);
throw new NotImplementedException();
case ShootTarget.DYNAMIC_SELECT_WINDOW:
throw new NotImplementedException();
default:
throw new ArgumentOutOfRangeException();
}
Console.Error.WriteLine(location);
Console.Error.WriteLine(size);
bitmap = new Bitmap(size.Width, size.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(location, new Point(), size);
}
/*
private void PaintPreview(Object sender, PaintEventArgs a) {
Console.Error.WriteLine(a);
a.Graphics.DrawImage(bitmap, 0, 0);
}
*/
private void 重新截图(Object o, EventArgs a) {
Size size = Size;
SuspendLayout();
Hide();
Application.DoEvents();
Thread.Sleep((int)delay_box.Value * 1000);
shoot_screen((ShootTarget)target_box.SelectedIndex);
预览区域.Image = bitmap;
Show();
Size = size;
ResumeLayout();
}
private void Quit(Object o, EventArgs a) {
//Environment.Exit(0);
Application.Exit();
}
private void Copy(Object o, EventArgs a) {
Console.Error.WriteLine(Clipboard.ContainsImage());
Console.Error.WriteLine(bitmap);
Clipboard.SetImage(bitmap);
//Clipboard.SetData("image/jpeg", bitmap);
Console.Error.WriteLine(Clipboard.ContainsImage());
}
[STAThreadAttribute]
public static int Main(String[] args) {
int delay = -1;
ShootTarget target = ShootTarget.FULL_SCREEN;
for(int i = 0; i < args.Length; i++) {
String a = args[i];
if(a.Length > 1 && a[0] == '-') {
switch(a) {
case "--delay":
if(++i >= args.Length) {
String msg = String.Format("Option '{0}' requires an argument",
a);
Console.Error.WriteLine(msg);
MessageBox.Show(msg, "Error");
return 255;
}
delay = Int32.Parse(args[i]);
break;
case "--select-area":
target = ShootTarget.SELECT_RECTANGLE;
break;
case "--help":
MessageBox.Show("--delay <n>\n--select-area", "Command Line Usage");
return 0;
default:
Console.Error.WriteLine("Invalid option " + a);
MessageBox.Show("Invalid option " + a, "Error");
return 255;
}
}
}
if(delay > 0) Thread.Sleep(delay * 1000);
MainForm f = new MainForm(target);
if(delay >= 0) f.delay_box.Value = delay;
Application.Run(f);
return 0;
}
}
}