How to adjust Brightness and Contrast of an image using a slider?

后端 未结 2 1196
耶瑟儿~
耶瑟儿~ 2020-12-11 08:58

I have an image and i want to adjust its brightness and contrast through a slider in xaml. I don\'t know where to start.Any help would be appreciated. I am trying to impleme

2条回答
  •  天命终不由人
    2020-12-11 09:57

    Btw, correct function for increasing brightness of premultiplied images is

        public static WriteableBitmap ChangeBrightness(WriteableBitmap source, int increment)
        {
            var dest = new WriteableBitmap(source.PixelWidth, source.PixelHeight);
    
            byte[] color = new byte[4];
    
            using (var srcBuffer = source.PixelBuffer.AsStream())
            using (var dstBuffer = dest.PixelBuffer.AsStream())
            {
                while (srcBuffer.Read(color, 0, 4) > 0)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        var value = (float)color[i];
                        var alpha = color[3] / (float)255;
                        value /= alpha;
                        value += increment;
                        value *= alpha;
    
                        if (value > 255)
                        {
                            value = 255;
                        }
    
                        color[i] = (byte)value;
                    }
    
                    dstBuffer.Write(color, 0, 4);
                }
            }
    
            return dest;
        }
    

提交回复
热议问题