Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. //Task
  2. public void DoWork()
  3.         {      
  4.             //Bitmap Array of tiles (Tiles = 3)
  5.             Bitmap[] splits = new Bitmap[tiles];
  6.  
  7.             //Bitmap for the Screenshot
  8.             var screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
  9.  
  10.             //Puffer Bitmap
  11.             Bitmap buffer = new Bitmap(screenshot.Width,screenshot.Height);
  12.  
  13.             //Calc tile size
  14.             int tilesize = screenshot.Width / tiles;
  15.  
  16.             // Create a graphics object from the bitmap.
  17.             var gfxScreenshot = Graphics.FromImage(screenshot);
  18.             while (isStarted)
  19.             {
  20.                 // Take the screenshot from the upper left corner to the right bottom corner.
  21.                 gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
  22.  
  23.                 // Use graphics object from buffer to draw tiles on
  24.                 using (Graphics g = Graphics.FromImage(buffer)) //Throws System.InvalidOperationException: "Das Objekt wird bereits an anderer Stelle verwendet." when touching the window
  25.                 {
  26.                     for (int i = 0; i < tiles; i++)
  27.                     {
  28.                         //Calculate dimensions for each tile
  29.                         Rectangle r = new Rectangle(tilesize * i, 0, tilesize, screenshot.Height);
  30.  
  31.                         //Copy pixels to bitmap array
  32.                         splits[i] = screenshot.Clone(r, PixelFormat.Format32bppArgb); //If code gets this far without System.InvalidOperationException it will throw a SystemOutOfMemoryException when over 4Gb.
  33.  
  34.                         //Draw result onto buffer
  35.                         g.FillRectangle(
  36.                             new SolidBrush(
  37.                                 ProcessBitmap(splits[i])) //returns a color
  38.                             ,r);
  39.                     }
  40.                    
  41.                 }
  42.                 //buffer.Save("Test.png", ImageFormat.Png);
  43.                 this.Invoke((MethodInvoker)delegate
  44.                 {
  45.                     ImageBox.Image = buffer;
  46.                 });
  47.             }
  48.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement