Adjusting image brightness

后端 未结 1 1510
一向
一向 2021-01-25 13:17

For windows phone app, when I am adjusting brightness by slider it works fine when I move it to right. But when I go back to previous position, instead of image darkening, it go

相关标签:
1条回答
  • 2021-01-25 13:48

    Your algorithm is wrong. Each time the slider's value changes, you're adding that value to the picture's brightness. What makes your logic flawed is that the value returned by the slider will always be positive, and you're always adding the brightness to the same picture.

    So, if the slider starts with a value of 10, I'll add 10 to the picture's brightness.

    Then, I slide to 5. I'll add 5 to the previous picture's brightness (the one you already added 10 of brightness to).

    Two ways to solve the issue:

    1. Keep a copy of the original picture, and duplicate it every time your method is called. Then add the brightness to the copy (and not the original). That's the safest way.

    2. Instead of adding the new absolute value of the slider, calculate the relative value (how much it changed since the last time the method was called:

      private double lastSliderValue;
      
      private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
      {
          var offset = slider1.Value - this.lastSliderValue;
          this.lastSliderValue = slider1.Value;
      
          // Insert your old algorithm here, but replace occurences of "slider1.Value" by "offset"
      }
      

    This second way can cause a few headaches though. Your algorithm is capping the RGB values to 255. In those cases, you are losing information and cannot revert back to the old state. For instance, take the extreme example of a slider value of 255. The algorithm sets all the pixels to 255, thus generating a white picture. Then you reduce the slider to 0, which should in theory restore the original picture. In this case, you'll subtract 255 to each pixel, but since every pixel's value is 255 you'll end up with a black picture.

    Therefore, except if you find a clever way to solve the issue mentionned in the second solution, I'd recommand going with the first one.

    0 讨论(0)
提交回复
热议问题