473,546 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.R eadWrite, PixelFormat.For mat24bppRgb);

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 3517
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???bgr bgrbgr???bgrbgr bgr??? 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???bgr bgrbgr???bgrbgr bgr???
^

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***@discussi ons.microsoft.c om> wrote in message
news:D8******** *************** ***********@mic rosoft.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.R eadWrite, PixelFormat.For mat24bppRgb);

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???bgr bgrbgr???bgrbgr bgr??? 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???bgr bgrbgr???bgrbgr bgr???
^

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***@discussi ons.microsoft.c om> wrote in message
news:D8******** *************** ***********@mic rosoft.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.R eadWrite, PixelFormat.For mat24bppRgb);

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***@discussi ons.microsoft.c om> wrote in message
news:CC******** *************** ***********@mic rosoft.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???bgr bgrbgr???bgrbgr bgr??? 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???bgr bgrbgr???bgrbgr bgr???
^

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***@discussi ons.microsoft.c om> wrote in message
news:D8******** *************** ***********@mic rosoft.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.R eadWrite, PixelFormat.For mat24bppRgb);
>
> 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
1652
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 (...); ... do things with obj1... STORE_POINTER (scriptObj1, obj1);
1
9490
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 worked pretty well, accepting to write unsafe code ;-) But my next struct has an array inside and I don't get it passed over to the DLL correctly. ...
7
2793
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 uiParam0; public uint uiParam1;
4
3822
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 tried to cast it to an IntPtr, but without success. And of course, on the receiever side, I need to cast it back from IntPtr to the function pointer,...
0
1734
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 will output. -- Start DLL code snippet private static extern unsafe Int16 GetHash(IntPtr Bitmap1, Int16 FilterNum, void* ImageHash); ///...
5
2231
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 language, require in input a pointer char: OpenFile(char* filename); To use this function in cSharp I have writing the following code:...
3
1432
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 the latter points to a const string literal. Now if I change the value of test2 like so- test2 = "Blah";
4
4389
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 '&a' are illegal because string is a managed type, while in the same case the 'int' type is available. Why can't i get the point or the address of a...
1
2168
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
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.