To start things off I'm fairly new to C#, XAML and windows phone. So far I have managed to get a photo into the app via gallery/camera and turn it into grayscale. But my aim is to get the rgb values from a pixel and display it under the image as text. How would I go about changing the grayscale code to instead modify the image to just display the rgb code? Thanks!
async void GrayScale(Image lpictureBox)
{
WriteableBitmap modifiedImage = lpictureBox.Source as WriteableBitmap;
if (modifiedImage == null)
{
BitmapSource bs = lpictureBox.Source as BitmapSource;
modifiedImage = new WriteableBitmap(bs.PixelWidth, bs.PixelHeight);
}
int r = 0, a = 0, i = 0;
int h = modifiedImage.PixelHeight;
int w = modifiedImage.PixelWidth;
Stream stream = modifiedImage.PixelBuffer.AsStream();
byte[] StreamBuffer = new byte[stream.Length];
stream.Seek(0, 0);
await stream.ReadAsync(StreamBuffer, 0, StreamBuffer.Length);
for (i = 0; i < StreamBuffer.Length - 4; i = i + 4)
{
var a1 = StreamBuffer[i + 3];
var r1 = StreamBuffer[i + 2];
var g1 = StreamBuffer[i + 1];
var b1 = StreamBuffer[i + 0];
a = 0xff;
r = (byte)((0.299 * r1) + (0.587 * g1) + (0.114 * b1));
StreamBuffer[i + 3] = (byte)a; // alpha
StreamBuffer[i + 2] = (byte)(r > 255 ? 255 : (r < 0 ? 0 : r)); //red
StreamBuffer[i + 1] = (byte)(r > 255 ? 255 : (r < 0 ? 0 : r)); //green
StreamBuffer[i + 0] = (byte)(r > 255 ? 255 : (r < 0 ? 0 : r)); //blue
}
stream.Seek(0, 0);
stream.Write(StreamBuffer, 0, StreamBuffer.Length);
modifiedImage.Invalidate();
lpictureBox.Source = modifiedImage;
}