471,627 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

ImageLockMode enumeration semantics

Hi,
the Bitmap.LockBits method takes the ImageLockMode enumeration as a
parameter. I may be missing something but if I supply a value of
ImageLockMode.ReadOnly to the LockBits method I am still able to change the
pixel values. Maybe I am missing something, but I would think that I should
not be able to change the values, or does this enumeration mean something
else?

Some sample code to set the Red component of an image to full for every
pixel, with the ReadOnly property passed into the LockBits method:

public unsafe void ProcessImage(Bitmap image)
{
Rectangle rectangle = new Rectangle(0, 0, image.Width,
image.Height);
BitmapData bitmapData = image.LockBits(rectangle,
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

//current pixel we are processing
byte* currentPixel = (byte*)bd.Scan0.ToPointer();

//end address when we should stop, which is
//start + number of pixels * 3 for 3bytes per pixel
byte* endPointer = currentPixel + image.Width * image.Height * 3;

//loop through all pixels in image
while (ptr != endPointer)
{
byte* endOfRow = currentPixel + bitmapData.Stride;

//loop through pixels in current row
while (currentPixel != endOfRow)
{
//set red component to full
currentPixel[2] = 255;

//move to next pixel
currentPixel += 3;
}

//move past padding to start of new row
currentPixel += bitmapData.Stride - (image.Width * 3);
}

//Put data back into bitmap object
image.UnlockBits(bd);
}
Thanks
Mark.
Jan 3 '06 #1
1 2061
sorry, here is a version of the code which actually compiles :-P

public unsafe void ProcessImage(Bitmap image)
{
Rectangle rectangle = new Rectangle(0, 0, image.Width,
image.Height);
BitmapData bitmapData = image.LockBits(rectangle,
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

//current pixel we are processing
byte* currentPixel = (byte*)bitmapData.Scan0.ToPointer();

//end address when we should stop, which is
//start + number of pixels * 3 for 3bytes per pixel
byte* endPointer = currentPixel + image.Width * image.Height * 3;

//loop through all pixels in image
while (currentPixel != endPointer)
{
byte* endOfRow = currentPixel + bitmapData.Stride;

//loop through pixels in current row
while (currentPixel != endOfRow)
{
//set red component to full
currentPixel[2] = 255;

//move to next pixel
currentPixel += 3;
}

//move past padding to start of new row
currentPixel += bitmapData.Stride - (image.Width * 3);
}

//Put data back into bitmap object
image.UnlockBits(bitmapData);
}

"Mark R. Dawson" wrote:
Hi,
the Bitmap.LockBits method takes the ImageLockMode enumeration as a
parameter. I may be missing something but if I supply a value of
ImageLockMode.ReadOnly to the LockBits method I am still able to change the
pixel values. Maybe I am missing something, but I would think that I should
not be able to change the values, or does this enumeration mean something
else?

Some sample code to set the Red component of an image to full for every
pixel, with the ReadOnly property passed into the LockBits method:

public unsafe void ProcessImage(Bitmap image)
{
Rectangle rectangle = new Rectangle(0, 0, image.Width,
image.Height);
BitmapData bitmapData = image.LockBits(rectangle,
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

//current pixel we are processing
byte* currentPixel = (byte*)bd.Scan0.ToPointer();

//end address when we should stop, which is
//start + number of pixels * 3 for 3bytes per pixel
byte* endPointer = currentPixel + image.Width * image.Height * 3;

//loop through all pixels in image
while (ptr != endPointer)
{
byte* endOfRow = currentPixel + bitmapData.Stride;

//loop through pixels in current row
while (currentPixel != endOfRow)
{
//set red component to full
currentPixel[2] = 255;

//move to next pixel
currentPixel += 3;
}

//move past padding to start of new row
currentPixel += bitmapData.Stride - (image.Width * 3);
}

//Put data back into bitmap object
image.UnlockBits(bd);
}
Thanks
Mark.

Jan 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Justin Wright | last post: by
8 posts views Thread by aevans1108 | last post: by
2 posts views Thread by Mark | last post: by
4 posts views Thread by Marshal | last post: by
1 post views Thread by Stefano G. | last post: by
3 posts views Thread by Davidoff | last post: by
reply views Thread by news.emn.fr | last post: by
3 posts views Thread by kenkahn | last post: by
reply views Thread by leo001 | last post: by

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.