473,327 Members | 2,069 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,327 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 2119
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
8
by: aevans1108 | last post by:
Greetings I can't seem to inherit enumerated values from a globally defined type in my XML schema. XmlSchema.Compile() doesn't like it. Here's the schema. <?xml version="1.0"...
2
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 ...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
1
by: Stefano G. | last post by:
I have a WSDL containing this enumeration type <xsd:simpleType name="item_type_enum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="VCD"/> <xsd:enumeration value="SVCD"/>...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
3
by: Davidoff | last post by:
Hi, I parse an XML file with a XSD schema. One XmlNode has an attribute whose type is a restriction of xs:string : <xs:simpleType name="stypeDay"> <xs:restriction base="xs:string">...
0
by: news.emn.fr | last post by:
Hello, i got this attribute <xs:attribute name="jour"> <xs:simpleType> <xs:restriction base="stypeJour"> </xs:restriction> </xs:simpleType> </xs:attribute>
3
by: kenkahn | last post by:
Given the following simple c program #include <stdio.h> enum ATTRTYPE { CFG_ATTR = 3000 }; int main() { #define xx(id) printf("ATTR=%s\n",#id);
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.