Advertisement
Guest User

Untitled

a guest
May 21st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1.         protected override void DoWork(IProgress<string> progress, CancellationToken cancellationToken)
  2.         {
  3.             cancellationToken.Register(SwitchToCancelledState);
  4.  
  5.             var processorCount = Environment.ProcessorCount;
  6.  
  7.             using (var bmp = new Bitmap(fileName))
  8.             {
  9.                 // Lock the bitmap's bits.  
  10.                 var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  11.                 var bmpData =
  12.                     bmp.LockBits(rect, ImageLockMode.ReadWrite,
  13.                         bmp.PixelFormat);
  14.  
  15.                 // Get the address of the first line.
  16.                 var ptr = bmpData.Scan0;
  17.  
  18.                 // Declare an array to hold the bytes of the bitmap.
  19.                 var bytes = Math.Abs(bmpData.Stride) * bmp.Height;
  20.                 var rgbValues = new byte[bytes];
  21.  
  22.                 // Copy the RGB values into the array.
  23.                 System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
  24.  
  25.                 var partSize = rgbValues.Length / processorCount;
  26.                 var oneQuarter = rgbValues.Length / 100 * 25;
  27.                 var i = -25;
  28.                 var counter = 0;
  29.  
  30.  
  31.                 for (var j = 0; j <= rgbValues.Length - partSize; j += partSize)
  32.                 {
  33.                     var start = j;
  34.                     var end = j + partSize - 1;
  35.  
  36.                     Task.Run(() =>
  37.                     {
  38.                         for (var k = start; k <= end; k++)
  39.                         {
  40.                             rgbValues[k] = (byte) (rgbValues[k] + brightnessChange);
  41.  
  42.                             Interlocked.Increment(ref counter);
  43.                             if (counter % oneQuarter != 0) return;
  44.                             i += 25;
  45.                             progress.Report($"Brightness job (ID {Id}) - {i}%");
  46.  
  47.                         }
  48.                     }, cancellationToken);
  49.  
  50.                 }
  51.  
  52.                 Task.WaitAll();
  53.  
  54.                 // Copy the RGB values back to the bitmap
  55.                 System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
  56.  
  57.                 // Unlock the bits.
  58.                 bmp.UnlockBits(bmpData);
  59.  
  60.                 // Save image
  61.                 bmp.Save(Paths.GetOutputImageFullName(Id, "brightness" + brightnessChange), ImageFormat.Jpeg);
  62.             }
  63.  
  64.             SwitchToFinishedState($"Changed brightness by {brightnessChange} point(s)");
  65.         }
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement