Pointer position in unsafe code

G

Guest

this code set the color that I want only on the first pixel.
I have an X & Y values of the pixel that I want to change.
How do I change the pointer's position according to my X & Y values that I
already have?


BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte * p = (byte *)(void *)Scan0;

int nOffset = stride - b.Width*3;
int nPixel;

nPixel = red;
nPixel = Math.Max(nPixel, 0);
p[2] = (byte)Math.Min(255, nPixel);

nPixel = green;
nPixel = Math.Max(nPixel, 0);
p[1] = (byte)Math.Min(255, nPixel);

nPixel =blue;
nPixel = Math.Max(nPixel, 0);
p[0] = (byte)Math.Min(255, nPixel);

p += 3;
}
 
K

Kevin Spencer

The code you posted DOES move the pointer position, right at the end (p+=3).
The bitmap is a set of XY coordinates, where the first pixel (p[0], p[1],
p[2]) is the first 3 bytes. A 2-dimensional bitmap is a 1-dimensional array
of bytes. So, the Y position is the column (stride * row), and the X
position is the column (column * 3). Each XY coordinate can then be
expressed as ((stride * Y) + (X * 3)). The nOffset variable in your code
represents the difference between the width of the bitmap and the stride, or
number of bytes per row. For example, let's say you have a 3X3 bitmap. This
is 3 pixels (of 3 bytes each) by 3 rows. Assuming, for example, a stride of
12 bytes per row, you would have a 1-dimentsional array of:

bgrbgrbgr???bgrbgrbgr???bgrbgrbgr??? or, represented as a 2-dimensional
matrix:

bgrbgrbgr???
bgrbgrbgr???
bgrbgrbgr???

The question marks represent the offset of the end of each row from the end
of the stride.

To find the pixel at (XY) (2, 1), you would use ((2 * 3) + (1 * 12)), or 18:

v
bgrbgrbgr???bgrbgrbgr???bgrbgrbgr???
^

The complete pixel would be that byte (blue), plus the two after it (green,
red).

You may have also noticed that the pixel order is reversed in bitmaps. Not
rgb, but bgr.
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
G

Guest

thx for your great answer.
but how can I change this code that it will take 2 parameters(X & Y) and
change the pixel value? van you help me write ir?

thx again!

Kevin Spencer said:
The code you posted DOES move the pointer position, right at the end (p+=3).
The bitmap is a set of XY coordinates, where the first pixel (p[0], p[1],
p[2]) is the first 3 bytes. A 2-dimensional bitmap is a 1-dimensional array
of bytes. So, the Y position is the column (stride * row), and the X
position is the column (column * 3). Each XY coordinate can then be
expressed as ((stride * Y) + (X * 3)). The nOffset variable in your code
represents the difference between the width of the bitmap and the stride, or
number of bytes per row. For example, let's say you have a 3X3 bitmap. This
is 3 pixels (of 3 bytes each) by 3 rows. Assuming, for example, a stride of
12 bytes per row, you would have a 1-dimentsional array of:

bgrbgrbgr???bgrbgrbgr???bgrbgrbgr??? or, represented as a 2-dimensional
matrix:

bgrbgrbgr???
bgrbgrbgr???
bgrbgrbgr???

The question marks represent the offset of the end of each row from the end
of the stride.

To find the pixel at (XY) (2, 1), you would use ((2 * 3) + (1 * 12)), or 18:

v
bgrbgrbgr???bgrbgrbgr???bgrbgrbgr???
^

The complete pixel would be that byte (blue), plus the two after it (green,
red).

You may have also noticed that the pixel order is reversed in bitmaps. Not
rgb, but bgr.
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

TomHL said:
this code set the color that I want only on the first pixel.
I have an X & Y values of the pixel that I want to change.
How do I change the pointer's position according to my X & Y values that I
already have?


BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte * p = (byte *)(void *)Scan0;

int nOffset = stride - b.Width*3;
int nPixel;

nPixel = red;
nPixel = Math.Max(nPixel, 0);
p[2] = (byte)Math.Min(255, nPixel);

nPixel = green;
nPixel = Math.Max(nPixel, 0);
p[1] = (byte)Math.Min(255, nPixel);

nPixel =blue;
nPixel = Math.Max(nPixel, 0);
p[0] = (byte)Math.Min(255, nPixel);

p += 3;
}
 
K

Kevin Spencer

The only thing I could do more than I already have at this point would be to
write your code for you. And that would not be right. My mission in life is
to help others to stand on their own, so that they will be successful at
what they do. If you rely on others to do your work for you, you will never
be able to do it for yourself.

You will need to study what you have read so far, including what I have
explained to you, until you understand it. Once you understand it, you will
not need someone else to write your code for you. Only in this way can you
be successful as a programmer.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

TomHL said:
thx for your great answer.
but how can I change this code that it will take 2 parameters(X & Y) and
change the pixel value? van you help me write ir?

thx again!

Kevin Spencer said:
The code you posted DOES move the pointer position, right at the end
(p+=3).
The bitmap is a set of XY coordinates, where the first pixel (p[0], p[1],
p[2]) is the first 3 bytes. A 2-dimensional bitmap is a 1-dimensional
array
of bytes. So, the Y position is the column (stride * row), and the X
position is the column (column * 3). Each XY coordinate can then be
expressed as ((stride * Y) + (X * 3)). The nOffset variable in your code
represents the difference between the width of the bitmap and the stride,
or
number of bytes per row. For example, let's say you have a 3X3 bitmap.
This
is 3 pixels (of 3 bytes each) by 3 rows. Assuming, for example, a stride
of
12 bytes per row, you would have a 1-dimentsional array of:

bgrbgrbgr???bgrbgrbgr???bgrbgrbgr??? or, represented as a 2-dimensional
matrix:

bgrbgrbgr???
bgrbgrbgr???
bgrbgrbgr???

The question marks represent the offset of the end of each row from the
end
of the stride.

To find the pixel at (XY) (2, 1), you would use ((2 * 3) + (1 * 12)), or
18:

v
bgrbgrbgr???bgrbgrbgr???bgrbgrbgr???
^

The complete pixel would be that byte (blue), plus the two after it
(green,
red).

You may have also noticed that the pixel order is reversed in bitmaps.
Not
rgb, but bgr.
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

TomHL said:
this code set the color that I want only on the first pixel.
I have an X & Y values of the pixel that I want to change.
How do I change the pointer's position according to my X & Y values
that I
already have?


BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte * p = (byte *)(void *)Scan0;

int nOffset = stride - b.Width*3;
int nPixel;

nPixel = red;
nPixel = Math.Max(nPixel, 0);
p[2] = (byte)Math.Min(255, nPixel);

nPixel = green;
nPixel = Math.Max(nPixel, 0);
p[1] = (byte)Math.Min(255, nPixel);

nPixel =blue;
nPixel = Math.Max(nPixel, 0);
p[0] = (byte)Math.Min(255, nPixel);

p += 3;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top