Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I use the following code to iterate through the data of a bitmap and XOR th value of the individual color bytes. I have tried to find a more effective way to do this.

I was hoping that it would be possible to XOR the entire pixel with for example an unsigned int (32bits) with value 0xFFFFFF00 to inverse the blue, green and red pixel values but leave the Alpha value intact. I have tried this with an unsigned int without success. I just end up with every fourth pixel inversed (or something else). Any ideas?

This is the code i use, works fine but how can i XOR the entire pixel not just the separate ARGB-Values?
byte XOR_MASK = 0xFF;
BitmapData bmpData = backBuffer.LockBits(rect, ImageLockMode.ReadWrite, pFormat);
for (int y = 0; y < bmpData.Height; y++) 
{   
	byte* row  = (byte *)bmpData.Scan0+(y*bmpData.Stride);
	for (int x = 0; x < bmpData.Width; x++) 
	{
		row[x*4]   = (byte)(row[x*4]   ^ XOR_MASK);  // Blue
		row[x*4+1] = (byte)(row[x*4+1] ^ XOR_MASK);  // Green
		row[x*4+2] = (byte)(row[x*4+2] ^ XOR_MASK);  // Red
                // Next byte Alpha value, leave intact
	}
}
// Unlock the bits.
backBuffer.UnlockBits(bmpData);
Posted

1 solution

C#
For (int I = 0; I < picture.width)
{
  For (int j = 0; j < picture.height; j++)
  { color C = picture.getpixel(I, j)
     Int x = C.ToArgb();
     X = X ^ key(or whatever u want to xor against the value of x)
     C = Color.FromArgb(X);
     Picture.SetPixel(I, j, C);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900