Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. var handle = new IntPtr();
  4. string title;
  5. FindWindowTitleMatch("notepad", out handle, out title);
  6. var bmp = CaptureApplication(handle);
  7. bmp.Save(@"C:test.bmp",ImageFormat.Bmp);
  8. var bmpfromfile = (Bitmap) Image.FromFile(@"C:test.bmp");
  9. var bytefromfile = ImageToByte(bmpfromfile);
  10. var bytefromSS = ImageToByte(bmp);
  11.  
  12.  
  13. Console.WriteLine(ByteArrayCompare(bytefromfile, bytefromSS));
  14. //false
  15. }
  16.  
  17. [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
  18. static extern int memcmp(byte[] b1, byte[] b2, long count);
  19.  
  20. static bool ByteArrayCompare(byte[] b1, byte[] b2)
  21. {
  22. // Validate buffers are the same length.
  23. // This also ensures that the count does not exceed the length of either buffer.
  24. return b1.Length == b2.Length && memcmp(b1, b2, b1.Length) == 0;
  25. }
  26.  
  27. public static byte[] ImageToByte(Image img)
  28. {
  29. ImageConverter converter = new ImageConverter();
  30. return (byte[])converter.ConvertTo(img, typeof(byte[]));
  31. }
  32.  
  33. public static Bitmap CaptureApplication(IntPtr handle)
  34. {
  35. var rect = new User32.Rect();
  36. User32.GetWindowRect(handle, ref rect);
  37.  
  38. int width = rect.right - rect.left;
  39. int height = rect.bottom - rect.top;
  40.  
  41. var bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  42. Graphics graphics = Graphics.FromImage(bmp);
  43. graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
  44. return bmp;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement