Click here to Skip to main content
15,913,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I take the RGB value of the selected region of the bitmap image using unsafe coding in .net
I need to change the particular region that I have selected and giving the i and j value of the starting position of the selected image.


The entire code is

//   selection is the rectangle from the mouse selected region from the picturebox....
bitmap bmp2 = new bitmap(picturebox1.image);
bitmapdata data = bmp2.lockbits(selection,ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
     byte *p = (byte*)(data.scan0);
     offset = data.stride - bmp.width*3;
     for(i=selection.x; i< limitx;i++)
     {
          for(j=selection.y;j<limity;j++)
          {
               position = j*bmp2.width *(3) +i*3+j*offset;//what is line used for i dot know....
               b = p [ position +j];     ///need to take the RGB value
               g = p [position+j+1];
               r = p [ position + j +2];
               p+=3;
          }
          p+=offset;
     }
}
picturebox.image = bmp2.image;   // that should display the selected region in any color....
bmp2.unlockbits(data);
bmp2.dispose();


It's not working properly.

Please help me...
Posted
Updated 3-Jan-11 22:56pm
v4
Comments
Dalek Dave 4-Jan-11 4:56am    
Edited for Grammar, Readability and Code Block.

The code as I see does nothing to the image. Looks like if you save the rgb values that you get in the loop into an array of some structure you get the rgb values for all the pixels, thats all. The above code in itself does not do anything.
If you want to get a rectangular section as a seprate image, the you can lock bits of that rectangle and read the scan0 of that data into a bitmap.
 
Share this answer
 
Comments
The M A R S 4-Jan-11 5:24am    
actually my purpose is to change the color where i selected in the huge image and repaint to another picturebox that should happen very fast so that i prefer unsafe coding as pointer
i just send the selected area as rectangle, the code should found the exact position of that image and process very quickly and returns that selected areas as different color please help me as code format...
This code will mess up things in case original bitmap pixel format is not PixelFormat.Format24bppRgb. The format should be checked up in all cases.

Despite of your intent to invert a subset of bits, this code actually invert all bits. Inverting a subset is trivial if it is a rectangle. In this case, the required rectangle should be passed as a first parameter of LockBits.

Actual update of the bitmap data is done by UnlockBits. That's why passing a rectangle of smaller size as a parameter of LockBits is always better is you want to have better performance.

This advice also valid of your subset is not rectangular. In this case, you can calculate bounding rectangle around the bits you're going to modify. If the interception of this bounding rectangle is less then the whole bitmap rectangle, you can have better performance again.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 3-Jan-11 23:28pm    
There is no actual question, so I decided just to report problems I can see.
The M A R S 4-Jan-11 0:17am    
i tried that too as
the entire code is

// selection is the rectangle from the mouse selected region from the picturebox....
bitmap bmp2 = new bitmap(picturebox1.image);
bitmapdata data = bmp2.lockbits(selection,ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte *p = (byte*)(data.scan0);
offset = data.stride - bmp.width*3;
for(i=selection.x; i< limitx;i++)
{
for(j=selection.y;j
Dalek Dave 4-Jan-11 4:56am    
Good Answer.
Sergey Alexandrovich Kryukov 4-Jan-11 13:07pm    
@9585228677:

Using a subset of a rectangular shape via passing this rectangle to LockbBts is always better. You see, actual update of bitmap data is done by the method UnlockBits; moving of data takes time, so the less the subset, the better.

Actually, let me put this in the text of my answer... please see updated Answer above.
Sergey Alexandrovich Kryukov 4-Jan-11 13:37pm    
From your text I don't understand if you still have a problem.
Maybe you don't see the result of updated bitmap on screen or something?

At them moment of writing, you already received 3 answer and still say "help me", you say what you want but you never told what's exactly going wrong.

Please tell this explicitly, if you need to solve the problem.
try
      {
        Bitmap bmp = new Bitmap(250, 50);
        //   selection is the rectangle from the mouse selected region from the picturebox....
        Bitmap bmp2 = new Bitmap(@"C:\Users\vinayakk\Desktop\tenant_notselected.jpg");
        BitmapData data = bmp2.LockBits(new Rectangle(0, 0, 250, 50), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        BitmapData dta1 = bmp.LockBits(new Rectangle(0, 0, 250, 50), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
      
      unsafe
      {
        //byte* p = (byte*)(data.Scan0);
        //var offset = data.Stride - bmp2.Width * 3;
        //for (int i = 10; i < 50; i++)
        //{
        //  for (int j = 10; j < 50; j++)
        //  {
        //    var position = j * bmp2.Width * (3) + i * 3 + j * offset;
        //    //what is line used for i dot know....               
        //    byte b = p[position + j] = (byte)255;
        //    // need to take the RGB value              
        //    byte g = p[position + j + 1] = (byte)255; byte r = p[position + j + 2] = (byte)255; p += 3;
        //  } p += offset;
        //}
      }
      // that should display the selected region in any color....
        dta1.Scan0 = data.Scan0;
        bmp2.UnlockBits(data);
        bmp.UnlockBits(dta1);
        
        bmp.Save("--Your path.jpg--");
      bmp2.Dispose();
      }
      catch (Exception)
      {
        throw;
      }


Change the paths. Your selection is saved. Uncomment the code in the loop the pixel colors are changed.
 
Share this answer
 
Comments
The M A R S 6-Jan-11 0:02am    
this wll save the selected area in the other new image but i need to do all operation in that image like we do red eye removal just mark that position and it remove the red mark in the same picture that i need using unsafe coding

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900