The problem is how to invert colors of a Silverlight Image element.
There is an Image with a JPG as a source. On a button click I need to invert the colors. Sounds simple, right. Take each pixel, then modify it's value by 255 - pixel value. But when I tried WritableBitmap loaded with the Image source, I got security exception disallowing pixel access. Here is my code:
if (MainLeftImage.Source != null)
{
WriteableBitmap bitmap = new WriteableBitmap((BitmapSource)MainLeftImage.Source);
byte[] pixels = new byte[bitmap.Pixels.Length];
int size = pixels.Count();
for (int i = 0; i < size; i++)
pixels[i] = (byte)(255 - pixels[i]);
bitmap.Invalidate();//redraw and then plug it back on
MainLeftImage.Source = bitmap;
}
}
catch (Exception ex)
{
}
Looks that WritableBitmap is not a solution, right? Any help appreciated. Thanks guys.