Advertisement
Guest User

C# ScreenShotter

a guest
Dec 11th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace ScreenShot_tool
  14. {
  15.     class NonStaticMain
  16.     {
  17.         [DllImport("user32.dll")]
  18.         static extern short GetAsyncKeyState(int vKey);
  19.  
  20.         public static bool IsKeyPushedDown(Keys vKey)
  21.         {
  22.             return 0 != (GetAsyncKeyState((int)vKey) & 0x8000);
  23.         }
  24.  
  25.         public NonStaticMain()
  26.         {
  27.             while (true)
  28.             {
  29.                 bool GotScreenShot = false;
  30.  
  31.                 int image_X = 0;//X Location
  32.                 int image_Y = 0;//Y Location
  33.                 int image_W = 0;//Width
  34.                 int image_H = 0;//Height
  35.  
  36.                 int originalX = Cursor.Position.X;
  37.                 int originalY = Cursor.Position.Y;
  38.                 while (IsKeyPushedDown(Keys.LButton))
  39.                 {
  40.                     Rectangle rect = new Rectangle();
  41.                     rect.X = Cursor.Position.X;
  42.                     rect.Y = Cursor.Position.Y;
  43.  
  44.                     Pen pen = new Pen(Color.Gray);
  45.  
  46.                     using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
  47.                     {
  48.                         int currentX = Cursor.Position.X;
  49.                         int currentY = Cursor.Position.Y;
  50.  
  51.                         //I'm sure t there's a more eficent way to do this. I'm just shit with maths.(I know it's not even complex maths :o)
  52.                         #region my shitty maths for drawing a dragable rectangle
  53.                         if (currentX >= originalX && currentY >= originalY)//bottom right
  54.                         {
  55.                             image_X = originalX;
  56.                             image_Y = originalY;
  57.                             image_W = (currentX - originalX);
  58.                             image_H = (currentY - originalY);
  59.                         }
  60.                         else if (currentX <= originalX && currentY <= originalY)//upper left
  61.                         {
  62.                             image_X = currentX;
  63.                             image_Y = currentY;
  64.                             image_W = (originalX - currentX);
  65.                             image_H = (originalY - currentY);
  66.                         }
  67.                         else if (currentX <= originalX)//bottom left
  68.                         {
  69.                             image_X = currentX;
  70.                             image_Y = originalY;
  71.                             image_W = (originalX - currentX);
  72.                             image_H = (currentY - originalY);
  73.                         }
  74.                         else if (currentY <= originalY)//top right
  75.                         {
  76.                             image_X = originalX;
  77.                             image_Y = currentY;
  78.                             image_W = (currentX - originalX);
  79.                             image_H = (originalY - currentY);
  80.                         }
  81.  
  82.                         graphics.DrawRectangle(
  83.                                 pen,
  84.                                 image_X,
  85.                                 image_Y,
  86.                                 image_W,
  87.                                 image_H);
  88.                         #endregion
  89.                     }
  90.  
  91.                     GotScreenShot = true;
  92.                 }
  93.  
  94.                 if (GotScreenShot)
  95.                 {
  96.                     using (Bitmap bitmap = new Bitmap(image_W, image_H))
  97.                     {
  98.                         using (Graphics graphics = Graphics.FromImage(bitmap))
  99.                         {
  100.                             graphics.CopyFromScreen(image_X, image_Y, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy);
  101.  
  102.                             uploadScreenShot(bitmap);
  103.                         }
  104.                     }
  105.                     break;
  106.                 }
  107.  
  108.                 Thread.Sleep(30);
  109.             }
  110.         }
  111.  
  112.         void uploadScreenShot(Bitmap image)
  113.         {
  114.             //cba adding upload code rn, so I'll just save it :P
  115.             string baseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\screenshots";
  116.  
  117.             if (!Directory.Exists(baseDirectory))
  118.                 Directory.CreateDirectory(baseDirectory);
  119.  
  120.             string fileLocation = baseDirectory + "\\" + randomString() + ".png";
  121.             image.Save(fileLocation, System.Drawing.Imaging.ImageFormat.Png);
  122.             Process.Start(baseDirectory);
  123.         }
  124.  
  125.         string randomString()
  126.         {
  127.             //Will return a random byte array in a string, cba making any other shit
  128.             Random rand = new Random();
  129.             string result = null;
  130.             for (int i = 0; i < rand.Next(20, 40); i++)
  131.                 result += ((byte)rand.Next(0, 255)).ToString("X");
  132.  
  133.             return result;
  134.         }
  135.     }
  136. }
  137. #region old code
  138. /*if (currentX >= originalX && currentY >= originalY)//bottom right
  139.                         {
  140.                             graphics.DrawRectangle(
  141.                                 pen,
  142.                                 originalX,
  143.                                 originalY,
  144.                                 (currentX - originalX),
  145.                                 (currentY - originalY));
  146.  
  147.                            
  148.                         }
  149.                         else if (currentX <= originalX && currentY <= originalY)//upper left
  150.                         {
  151.                             graphics.DrawRectangle(
  152.                                 pen,
  153.                                 currentX,
  154.                                 currentY,
  155.                                 (originalX - currentX),
  156.                                 (originalY - currentY));
  157.                         }
  158.                         else if (currentX <= originalX)//bottom left
  159.                         {
  160.                             graphics.DrawRectangle(
  161.                                 pen,
  162.                                 currentX,
  163.                                 originalY,
  164.                                 (originalX - currentX),
  165.                                 (currentY - originalY));
  166.                         }
  167.                         else if (currentY <= originalY)//top right
  168.                         {
  169.                             graphics.DrawRectangle(
  170.                                 pen,
  171.                                 originalX,
  172.                                 currentY,
  173.                                 (currentX - originalX),
  174.                                 (originalY - currentY));
  175.                         }*/
  176. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement