473,386 Members | 1,712 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,386 software developers and data experts.

Direct memory-access GetPixel()-method returns 0 instead of 255

Does anyone have any idea why I can't get a pixel-value of 255 returned in
the code below?
Any color-value from 0 up to 254 is returned correctly, but any
color-component of 255 returns zero. ie. bright white pixel would return
0,0,0 with the function below: The color (234,123,255) returns (234,123,0).

I would have thought that the byte value could hold values from 0..255 and
not only 0..254. At least thats what the C# specs and my common sense tell
me :-)

/// <summary>
/// Gets the color of a 24bit-pixel by using direct access to memory
/// </summary>
/// <param name="x">Row</param>
/// <param name="y">Column</param>
/// <returns>Color of pixel at (row,column)</returns>
public unsafe Color GetPixel(int x, int y)
{
byte* p = (byte *)bmd.Scan0 + y*bmd.Stride + 3*x;
return Color.FromArgb(p[2],p[1],p[0]);
}

where bmd is declared as:
private BitmapData bmd = myBitmap.LockBits(new Rectangle(0, 0,
myBitmap.Width, myBitmap.Height), ImageLockMode.ReadWrite,
myBitmap.PixelFormat);

I have checked that (myBitmap.PixelFormat==PixelFormat.Format24bppRgb) is
true.

Any help or ideas are appreciated

Regards
/Morten Nielsen
www.dotnetgis.net
Nov 16 '05 #1
1 5568
Hi Morten,

Using your exact same code, I do get 255s!

Using a 4x4 test bmp with several colors (including black, white, red,
green, blue, magenta, cyan and yellow), with a textbox called "tb" :

BitmapData bmd ;

public unsafe Color GetPixel(int x, int y)
{
byte* p = (byte *)bmd.Scan0 + y*bmd.Stride + 3*x;
return Color.FromArgb(p[2],p[1],p[0]);
}

private void button1_Click(object sender, EventArgs e)
{
Bitmap myBitmap = new Bitmap("C:\\test.bmp");
bmd = myBitmap.LockBits(new Rectangle(0, 0,myBitmap.Width,
myBitmap.Height), ImageLockMode.ReadWrite, myBitmap.PixelFormat);

for (int y = 0 ; y < 4 ; y++)
{
for (int x = 0 ; x < 4 ; x++)
{
tb.AppendText(GetPixel(x,y) + Environment.NewLine);
}
}
}

This returns :

Color [A=255, R=255, G=255, B=255]
Color [A=255, R=0, G=0, B=0]
Color [A=255, R=255, G=0, B=0]
Color [A=255, R=255, G=255, B=0]
Color [A=255, R=0, G=255, B=0]
Color [A=255, R=0, G=255, B=255]
Color [A=255, R=0, G=0, B=255]
Color [A=255, R=255, G=0, B=255]
<snip>

So I believe that your error lies not in your reading routine, which
is fine, but in your "color returns". How exactly do you read the
color's values?

HTH,

Michel

"Morten Nielsen" <no@spam.thx> wrote in message news:<ud**************@TK2MSFTNGP10.phx.gbl>...
Does anyone have any idea why I can't get a pixel-value of 255 returned in
the code below?
Any color-value from 0 up to 254 is returned correctly, but any
color-component of 255 returns zero. ie. bright white pixel would return
0,0,0 with the function below: The color (234,123,255) returns (234,123,0).

I would have thought that the byte value could hold values from 0..255 and
not only 0..254. At least thats what the C# specs and my common sense tell
me :-)

/// <summary>
/// Gets the color of a 24bit-pixel by using direct access to memory
/// </summary>
/// <param name="x">Row</param>
/// <param name="y">Column</param>
/// <returns>Color of pixel at (row,column)</returns>
public unsafe Color GetPixel(int x, int y)
{
byte* p = (byte *)bmd.Scan0 + y*bmd.Stride + 3*x;
return Color.FromArgb(p[2],p[1],p[0]);
}

where bmd is declared as:
private BitmapData bmd = myBitmap.LockBits(new Rectangle(0, 0,
myBitmap.Width, myBitmap.Height), ImageLockMode.ReadWrite,
myBitmap.PixelFormat);

I have checked that (myBitmap.PixelFormat==PixelFormat.Format24bppRgb) is
true.

Any help or ideas are appreciated

Regards
/Morten Nielsen
www.dotnetgis.net

Nov 16 '05 #2

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

Similar topics

2
by: Jason Hirst | last post by:
Hi, I'm not sure if this is possible, I can't see why, but is there a way round a problem I have. I'm returning the results from a stored procedure, of which there could be anything from 5 to...
10
by: Alex Greem | last post by:
Dear all, Our database (DB2 Workgroup 7.2 FP12) is constantly under heavy load. Most time CPU usage (1 Pentium3 1Ghz) is more 50% busy. We have 3GB RAM memory Our normal workload is 200-300...
5
by: Bruce Pullen | last post by:
DB2 7.2 (ESE) on AIX 5.2. In 3 weeks, we've seen the following : Tablespace name = SYSCATSPACE Direct writes = 150306 Direct write elapsed...
5
by: Nadav | last post by:
Hi, I am trying to get a direct pointer to the address of the screen ( something like 0x0b00 in the happy DOS days ), how can I do that? Should this be done in the kernel or it can also be done...
1
by: Pavils Jurjans | last post by:
Hello, My ASP.NET hoster has made a separate folder in my hosting space and configured it as separate application in IIS. Further, I created "bin" folder in this directory, and put in my aspx...
21
by: Chen Shusheng | last post by:
Hello, I have a small piece of code that I want to directly assign a segment of memory to a pointer. But complier tell me wrong. Pls you help.Codes below: ------------------ int * p;...
5
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): ...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
5
by: Rahul B | last post by:
Hi, I am having the following issues while trying to restrict the current user from creating any objects. Below is the privileges for the user and response when i try to create a table in that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.