using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Drawing.Imaging;
namespace G {
public partial class Controller : Form {
public Controller() {
InitializeComponent();
}
private MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
private ImageConverter c = new ImageConverter();
private FrameX f = null;
private void Controller_Load(object sender, EventArgs e) {
f = new FrameX(this);
f.Visible = true;
}
private void setCam(object sender, EventArgs e) {
f.r = new Rectangle( //
Int32.Parse(this.Kt.Text), Int32.Parse(this.Kl.Text), //
Int32.Parse(this.Kx.Text), Int32.Parse(this.Ky.Text));
f.Refresh();
}
public class FrameX : Form {
private Form parent = null;
public FrameX(Form parent) {
this.parent = parent;
this.SuspendLayout();
this.FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = System.Drawing.SystemColors.Control;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Frame_Paint);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Frame_MouseClick);
this.Enabled = false;
this.ResumeLayout(false);
}
private void Frame_MouseClick(object sender, MouseEventArgs e) {
this.parent.Focus();
}
public Rectangle r = new Rectangle(151, 181, 640, 360);
private const int w = 3;
private void Frame_Paint(object sender, PaintEventArgs e) {
using (Graphics g = e.Graphics)
using (Pen p = new Pen(Color.Black, w))
using (Brush b = new SolidBrush(SystemColors.Control))
{
g.Clear(SystemColors.GrayText);
g.DrawRectangle(p, r.X - w + 1, r.Y - w + 1, r.Width + w, r.Height + w);
g.FillRectangle(b, r);
}
}
}
private void isCamVisible_CheckedChanged(object sender, EventArgs e) {
this.Deactivate -= new System.EventHandler(this.Controller_Deactivate);
f.Visible = this.isCamVisible.Checked;
this.Deactivate += new System.EventHandler(this.Controller_Deactivate);
this.Focus();
}
private void Controller_Deactivate(object sender, EventArgs e) {
this.isCamVisible.Checked=false;
}
private void doValidating(object sender, CancelEventArgs e) {
TextBox p = (TextBox)sender;
int nr = Int32.Parse(p.Text);
errorProvider1.SetError(p, (nr < 0 || nr > 1000 ? "Off bounds." : ""));
}
private Bitmap getContentBitmap() {
Rectangle r = f.r;
Bitmap hc = new Bitmap(r.Width, r.Height);
using (Graphics gf = Graphics.FromImage(hc)) {
gf.CopyFromScreen(r.Left, r.Top, 0, 0, //
new Size(r.Width, r.Height), CopyPixelOperation.SourceCopy);
}
return hc;
}
private byte[] getBitmapHash(Bitmap hc) {
return md5.ComputeHash(c.ConvertTo(hc, typeof(byte[])) as byte[]);
}
public static Bitmap getGrayscale(Bitmap hc){
Bitmap result = new Bitmap(hc.Width, hc.Height);
ColorMatrix colorMatrix = new ColorMatrix(new float[][]{
new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0},
new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0,0,0,1,0,0},
new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}});
using (Graphics g = Graphics.FromImage(result)) {
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(hc, new Rectangle(0, 0, hc.Width, hc.Height),
0, 0, hc.Width, hc.Height, GraphicsUnit.Pixel, attributes);
}
return result;
}
private void getHash_Click(object sender, EventArgs e) {
DateTime a = DateTime.Now;
Bitmap b = getContentBitmap();
String o = String.Join(",", getBitmapHash(b));
//getGrayscale(b).Save(@"C:\Users\Margus Martsepp\Desktop\out.png", ImageFormat.Png);
DateTime l = DateTime.Now;
Console.WriteLine(String.Format("Hash is: {0} [{1}ms]", o, (int)(l - a).TotalMilliseconds));
}
}
}