Advertisement
Guest User

Margus g code

a guest
Oct 23rd, 2010
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.33 KB | None | 0 0
  1.     using System;
  2.     using System.Collections.Generic;
  3.     using System.ComponentModel;
  4.     using System.Data;
  5.     using System.Drawing;
  6.     using System.Linq;
  7.     using System.Text;
  8.     using System.Windows.Forms;
  9.     using System.Security.Cryptography;
  10.     using System.Drawing.Imaging;
  11.  
  12.     namespace G {
  13.         public partial class Controller : Form {
  14.             public Controller() {
  15.                 InitializeComponent();
  16.             }
  17.  
  18.             private MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  19.             private ImageConverter c = new ImageConverter();
  20.             private FrameX f = null;
  21.             private void Controller_Load(object sender, EventArgs e) {
  22.                 f = new FrameX(this);
  23.                 f.Visible = true;
  24.             }
  25.  
  26.             private void setCam(object sender, EventArgs e) {
  27.                 f.r = new Rectangle( //
  28.                     Int32.Parse(this.Kt.Text), Int32.Parse(this.Kl.Text), //
  29.                     Int32.Parse(this.Kx.Text), Int32.Parse(this.Ky.Text));
  30.                 f.Refresh();
  31.             }
  32.  
  33.             public class FrameX : Form {
  34.                 private Form parent = null;
  35.                 public FrameX(Form parent) {
  36.                     this.parent = parent;
  37.                     this.SuspendLayout();
  38.                     this.FormBorderStyle = FormBorderStyle.None;
  39.                     this.TransparencyKey = System.Drawing.SystemColors.Control;
  40.                     this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
  41.                     this.Paint += new System.Windows.Forms.PaintEventHandler(this.Frame_Paint);
  42.                     this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Frame_MouseClick);
  43.                     this.Enabled = false;
  44.                     this.ResumeLayout(false);
  45.                 }
  46.  
  47.                 private void Frame_MouseClick(object sender, MouseEventArgs e) {
  48.                     this.parent.Focus();
  49.                 }
  50.  
  51.                 public Rectangle r = new Rectangle(151, 181, 640, 360);
  52.                 private const int w = 3;
  53.                 private void Frame_Paint(object sender, PaintEventArgs e) {
  54.                     using (Graphics g = e.Graphics)
  55.                     using (Pen p = new Pen(Color.Black, w))
  56.                     using (Brush b = new SolidBrush(SystemColors.Control))
  57.                     {
  58.                         g.Clear(SystemColors.GrayText);
  59.                         g.DrawRectangle(p, r.X - w + 1, r.Y - w + 1, r.Width + w, r.Height + w);
  60.                         g.FillRectangle(b, r);
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             private void isCamVisible_CheckedChanged(object sender, EventArgs e) {
  66.                 this.Deactivate -= new System.EventHandler(this.Controller_Deactivate);
  67.                 f.Visible = this.isCamVisible.Checked;
  68.                 this.Deactivate += new System.EventHandler(this.Controller_Deactivate);
  69.                 this.Focus();
  70.             }
  71.  
  72.             private void Controller_Deactivate(object sender, EventArgs e) {
  73.                 this.isCamVisible.Checked=false;
  74.             }
  75.  
  76.             private void doValidating(object sender, CancelEventArgs e) {
  77.                 TextBox p = (TextBox)sender;
  78.                 int nr = Int32.Parse(p.Text);
  79.                 errorProvider1.SetError(p, (nr < 0 || nr > 1000 ? "Off bounds." : ""));
  80.             }
  81.  
  82.             private Bitmap getContentBitmap() {
  83.                 Rectangle r = f.r;
  84.                 Bitmap hc = new Bitmap(r.Width, r.Height);
  85.                 using (Graphics gf = Graphics.FromImage(hc)) {
  86.                     gf.CopyFromScreen(r.Left, r.Top, 0, 0, //
  87.                         new Size(r.Width, r.Height), CopyPixelOperation.SourceCopy);
  88.                 }
  89.                 return hc;
  90.             }
  91.  
  92.             private byte[] getBitmapHash(Bitmap hc) {
  93.                 return md5.ComputeHash(c.ConvertTo(hc, typeof(byte[])) as byte[]);
  94.             }
  95.  
  96.             public static Bitmap getGrayscale(Bitmap hc){
  97.                 Bitmap result = new Bitmap(hc.Width, hc.Height);
  98.                 ColorMatrix colorMatrix = new ColorMatrix(new float[][]{  
  99.                     new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0},
  100.                     new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0,0,0,1,0,0},
  101.                     new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}});
  102.  
  103.                 using (Graphics g = Graphics.FromImage(result)) {
  104.                     ImageAttributes attributes = new ImageAttributes();
  105.                     attributes.SetColorMatrix(colorMatrix);
  106.                     g.DrawImage(hc, new Rectangle(0, 0, hc.Width, hc.Height),
  107.                        0, 0, hc.Width, hc.Height, GraphicsUnit.Pixel, attributes);
  108.                 }
  109.                 return result;
  110.             }
  111.  
  112.             private void getHash_Click(object sender, EventArgs e) {
  113.                 DateTime a = DateTime.Now;
  114.                 Bitmap b = getContentBitmap();
  115.                 String o = String.Join(",", getBitmapHash(b));
  116.                 //getGrayscale(b).Save(@"C:\Users\Margus Martsepp\Desktop\out.png", ImageFormat.Png);
  117.                 DateTime l = DateTime.Now;
  118.  
  119.                 Console.WriteLine(String.Format("Hash is: {0} [{1}ms]", o, (int)(l - a).TotalMilliseconds));
  120.             }
  121.         }
  122.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement