1

I want to adjust the contrast of an image in Windows Phone 8.1 I have found samples for Windows Phone 7 but it does not work in Windows Phone 8.1.

This is my code:

    public static WriteableBitmap ChangeContrast(WriteableBitmap Bitmap,
                                                    int[] orgPixels,
                                                    double contrastValue)
    {
        var result = new WriteableBitmap(Bitmap.PixelWidth, Bitmap.PixelHeight);
        var contrastFactor = (50f + contrastValue) / 50f;
        contrastFactor *= contrastFactor;
        var contrastFactorIndex = (int)(contrastFactor * 32768);
        var inputPixels = Bitmap.PixelBuffer;
        int[] tempPixels = new int[Bitmap.PixelBuffer.Length];
        orgPixels.CopyTo(tempPixels, 0);
        if (0 != Bitmap.PixelBuffer.Length)
        {
            for (int index = 0; index < Bitmap.PixelBuffer.Length; index++)
            {
                // Extract color components
                var color = tempPixels[index];
                var alpha = (byte)(color >> 24);
                var red = (byte)(color >> 16);
                var green = (byte)(color >> 8);
                var blue = (byte)(color);
                int ri = red - 128;
                int gi = green - 128;
                int bi = blue - 128;

                // Multiply contrast factor
                ri = (ri * contrastFactorIndex) >> 15;
                gi = (gi * contrastFactorIndex) >> 15;
                bi = (bi * contrastFactorIndex) >> 15;

                // Transform back to range [0, 255]
                ri = ri + 128;
                gi = gi + 128;
                bi = bi + 128;
                // Clamp to byte boundaries
                red = (byte)(ri > 255 ? 255 : (ri < 0 ? 0 : ri));
                green = (byte)(gi > 255 ? 255 : (gi < 0 ? 0 : gi));
                blue = (byte)(bi > 255 ? 255 : (bi < 0 ? 0 : bi));
                result.SetValue[index] = (alpha << 24) | (red << 16) | (green << 8) | blue;
            }
        }

//additional Buffer.BlockCopy(tempPixels, 0, inputPixels, 0, tempPixels.Length);

        return result;
    }

The 'SetValue' was 'pixels' in the original sample but Pixels does not exist in this object on WP8.1.

What is the equivalent please?

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 1
    The following imaging sdk has the filtering that your are trying to use plus much much more http://developer.nokia.com/lumia/nokia-apis/imaging – Stuart Smith Dec 19 '14 at 21:37

1 Answers1

1

You want to modify the int[] tempPixels array. Once you are finished with that you want to copy the tempPixel array back to the result.PixelBuffer. You can use Buffer.BlockCopy to copy a block to another block.

Full example: how to adjust brightness: Adjust Brightness WriteableBitmap 8.1

Full example: grayscale an image: Grayscale an Image WriteableBitmap 8.0


Remember to add in System.IO and System.Runtime.InteropServices.WindowsRuntime for the solutions above to work.

I also included the full 8.1 project here on my OneDrive: Adjust Brightness Project 8.1


Community
  • 1
  • 1
Chubosaurus Software
  • 8,133
  • 2
  • 20
  • 26