473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Pointer position in unsafe code

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;
}
Nov 17 '05 #1
3 3509
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" <To***@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
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;
}

Nov 17 '05 #2
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" wrote:
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" <To***@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
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;
}


Nov 17 '05 #3
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" <To***@discussions.microsoft.com> wrote in message
news:CC**********************************@microsof t.com...
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" wrote:
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" <To***@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
> 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;
> }
>
>


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William Djaja Tjokroaminata | last post by:
Hi, As I am interfacing my code in C++ with a scripting language which is written in C, I need to store pointers to objects in a class hierarchy: ParentClass *obj1; obj1 = new ParentClass...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
4
by: Abra | last post by:
I have an application where I need to send a inter-process message (a data stream) that contains among other the address of a function (member of a class). For that, I need to serialize it, so I...
0
by: bcutting | last post by:
I have the following snippet of code which which makes a call into a dll to generate an array of bytes. Its arguments are a IntPtr to the bitmap, a number, and a reference to the array that it...
5
by: Sheikko | last post by:
Hi, friends. I am new in this group and I have a problem. I am just started to program in cSharp and I need a help. I need to import a ddl and to use one of its function. The function, in C...
3
by: Vishesh | last post by:
I have often seen in various sourcecodes that isntead a normal char array they use a pointer to a char. Like Instead of char test = "C++"; char * test2 = "C++"; I realize the difference that...
4
by: =?Utf-8?B?ZmFpcnl2b2ljZQ==?= | last post by:
Hi, I want to ask some basic question about the string type. When compiling the unsafe code: --------- string a = "wori"; string* b = &a; --------- it pops up errors saying both 'string*b' and...
1
by: couturep | last post by:
Hi, I need your precious time to help me to resolve this damn problem i have. I want to get a pointer from an array of struct. CODE : unsafe public struct TECCPARAM {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.