How to get a RGB values from an image buffer?
-
Hi, I am new to C# and im working trying some image processing, im using this bit of code to invert the pixels on an image grabbed from a video file, how do I extract the RGB values from a pixel in C# using the image buffer? Speed is of the essence... Thanks in advance.
unsafe
{
byte* p = (byte *)(void *)pBuffer;
int nWidth = Width * 3;
for(int y = 0; y < Height; y++)
{
for(int x = 0; x < nWidth; x++ )
{
p[0] = (byte)(255-p[0]);
p++; -
Hi, I am new to C# and im working trying some image processing, im using this bit of code to invert the pixels on an image grabbed from a video file, how do I extract the RGB values from a pixel in C# using the image buffer? Speed is of the essence... Thanks in advance.
unsafe
{
byte* p = (byte *)(void *)pBuffer;
int nWidth = Width * 3;
for(int y = 0; y < Height; y++)
{
for(int x = 0; x < nWidth; x++ )
{
p[0] = (byte)(255-p[0]);
p++; -
Hi, I am new to C# and im working trying some image processing, im using this bit of code to invert the pixels on an image grabbed from a video file, how do I extract the RGB values from a pixel in C# using the image buffer? Speed is of the essence... Thanks in advance.
unsafe
{
byte* p = (byte *)(void *)pBuffer;
int nWidth = Width * 3;
for(int y = 0; y < Height; y++)
{
for(int x = 0; x < nWidth; x++ )
{
p[0] = (byte)(255-p[0]);
p++;This is how you get the color components of a pixel, specified by the x and y coordinates:
byte* p = (byte *)(void *)pBuffer;
p += (x + y * Width) * 3;
byte r = *(p++);
byte g = *(p++);
byte b = *p;If you increase the pointer for the last component also, it will point to the next pixel. This is very useful if you access the pixels in sequence, as you don't have to calculate a new address for each pixel.
Despite everything, the person most likely to be fooling you next is yourself.