I have been trying to load an image (which works) as a bitmapimage then convert that writeablebitmap to grey scale by averaging all of the colours together how ever when ever i do it the image has green or blue and red line across seemly dependent if the offset was even or odd. I manged to change that so its white lines with weird messed up RGB across sections of the image. I dont know if its my code a common problem with writeablebitmap.
images
this is what the image i am loading into the program looks like
this is the image after i run my program
code
void imageLoad()
{
BitmapImage bit = new BitmapImage();
bit.BeginInit();
bit.UriSource = new Uri(@"amazon.jpg", UriKind.Relative);
bit.EndInit();
storeinarray(bit);
}
void storeinarray(BitmapImage bit)
{
WriteableBitmap wrbit = new WriteableBitmap(bit);
int pxWidth = wrbit.PixelWidth;
int pxHeigth = wrbit.PixelHeight;
int numColours = pxWidth * pxHeigth * 4;
int[,][] colourData = new int[pxWidth, pxHeigth][];
int stride =(bit.PixelWidth * bit.Format.BitsPerPixel+7) / 8;
byte[] array = new byte[stride * bit.PixelHeight];
bit.CopyPixels(array, stride, 0);
for (int x = 0; x < pxWidth; x++)
{
for (int y = 0; y < pxHeigth*4; y+=4)
{
colourData[x, y/4] = new int[4];
colourData[x, y/4][0] = array[x + y];
colourData[x, y/4][1] = array[x + y + 1];
colourData[x, y/4][2] = array[x + y + 2];
colourData[x, y/4][3] = array[x + y + 3];
}
}
changeImage(colourData, bit, stride);
}
void changeImage(int[,][] colourdata, BitmapImage bit, int stride)
{
int avg = 0;
WriteableBitmap writeable = new WriteableBitmap(bit);
Int32Rect int32rect = new Int32Rect(x: 0, y: 0, width: writeable.PixelWidth, height: writeable.PixelHeight);
byte[] dataforwr = new byte[stride * bit.PixelHeight];
Random ran = new Random();
for (int x = 0; x < colourdata.GetLength(0); x++)
{
for (int y = 0; y < colourdata.GetLength(1); y++)
{
avg = ((colourdata[x, y][0] + colourdata[x, y][1] + colourdata[x, y][2]) / 3);
colourdata[x, y][0] = colourdata[x, y][0];//B
colourdata[x, y][1] = colourdata[x, y][1];//G
colourdata[x, y][2] = colourdata[x, y][2]; //R
colourdata[x, y][3] = 255;//A
}
}
int xs = 0;
int ys = 0;
for (int i = 0; i < dataforwr.Length / 4; i += 4)
{
dataforwr[i] = (byte)colourdata[xs, ys][0];
dataforwr[i + 1] = (byte)colourdata[xs, ys][1];
dataforwr[i + 2] = (byte)colourdata[xs, ys][2];
dataforwr[i + 3] = (byte)colourdata[xs, ys][3];
ys++;
if (ys + 1 == writeable.PixelHeight)
{
xs++;
ys = 0;
}
}
int buffersize = stride * writeable.PixelHeight;
writeable.WritePixels(int32rect, dataforwr, stride, 0);
Image iamge = new Image();
iamge.Source = bit;
Canvas.SetTop(iamge, 100);
can.Children.Add(iamge);
Image image = new Image();
image.Source = writeable;
// image.Stretch = Stretch.UniformToFill;
can.Children.Add(image);
}
I have tried a solid block of colour for each pixel and all the pixel come out the same colour but then mix where there are green line and such e.g i set a solid colour red so A=255,R=255,B=0,G=0 the back ground would be red with yellow lines where the green and red have been put together