0

I am new to Windows Phone * development,in my project I need to convert the pixel of image to either Red,Green,Violet,etc colors as per my requirements.Can anyone send a sample code to do this process?

It would be a great help.

Thanks.

  • I checked the following links,but didn't help me http://tech.pro/tutorial/660/csharp-tutorial-convert-a-color-image-to-grayscale –  Oct 17 '14 at 06:25
  • just to be clear, you are working on WP8 silverlight project or the winRT?? – Samy S.Rathore Oct 17 '14 at 06:28
  • check [this](http://dotnet.dzone.com/articles/altering-pixels-windows-phone) one out. What didn't worked with the link you mentioned?? looks good to me. – Samy S.Rathore Oct 17 '14 at 06:31
  • Thanks,please also send more links if you get,In the link I sent there,I got error at using Bitmap,there was no class as Bitmap in WP8 –  Oct 17 '14 at 06:33

1 Answers1

0

See my solution here for converting an image to gray scale.

Convert A Dynamic Bitmap To Gray Scale.


Play attention to the loop where I decode each Red,Green,Blue pixel out of the Pixel array and where I reassemble the picture back with Buffer.BlockCopy. So basically you can do whatever you want with those components to modify your picture then BlockCopy it back.


private void PutPixel(int x, int y, byte red, byte green, byte blue, ref WriteableBitmap wb)
{
    if(wb == null)
        return;
    int[] PixelsBuffer = new int[1];
    unchecked
    {
        PixelsBuffer[0] = (int)( 0xFF000000 | (int)red << 16 | (int)green << 8 | blue);
    }
    int offset = ((y * wb.PixelWidth) + x) * 4;
    Buffer.BlockCopy(PixelsBuffer, 0, wb.Pixels, offset, 4);            
}

Code In Action

<Image x:Name="myImage" Source="/Assets/AlignmentGrid.png" Stretch="None" />

// draw a white line from (0,0) to (100, 100)    
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    WriteableBitmap wb = new WriteableBitmap((BitmapImage)this.myImage.Source);
    for (int i = 0; i < 100; i++)
    {
        PutPixel(i, i, 0xFF, 0xFF, 0xFF, ref wb);
    }
    myImage.Source = wb;
}

enter image description here

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