Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, I found this code in Image Processing for Dummies with C# and GDI+ Part 1 - Per Pixel Filters[^]

I found I can't understand code, here this code:

public static bool Invert(Bitmap b)
{
    // GDI+ still lies to us - the return format is BGR, NOT RGB. 

    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), 
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
    int stride = bmData.Stride; 
    System.IntPtr Scan0 = bmData.Scan0; 
    unsafe 
    { 
        byte * p = (byte *)(void *)Scan0;
        int nOffset = stride - b.Width*3; 
        int nWidth = b.Width * 3;
        for(int y=0;y < b.Height;++y)
        {
            for(int x=0; x < nWidth; ++x )
            {                                       
                p[0] = (byte)(255-p[0]);    // Here I want to get by pixel(x,y)
                ++p;                        // position simultaneously for exp:
                                            // result= 
                                            // pixel(0,0)+pixel(1,0)+pixel(2,0)
                                            // +pixel(0,1)+pixel(1,1)+ pixel(2,1)
                                            // +pixel(0,2)+pixel(1,2)+ pixel(2,2)
                                            // result=result/9;
                                            

            }           
            p += nOffset;                   //for what this function "p+=nOffset"?
        }                                   //I try to disable it and still work
    }
    b.UnlockBits(bmData);
    return true;
}


Here I want to get by pixel(x,y) position simultaneously for exp:
result= pixel(0,0)+pixel(1,0)+pixel(2,0)+pixel(0,1)+pixel(1,1)+ pixel(2,1)+pixel(0,2)+pixel(1,2)+ pixel(2,2)result=result/9;
How to do it?

and than I don't understand with "p += nOffset;", I try disable it and still running. please explain to me?

thanks :D
Posted
Updated 30-Jun-11 22:44pm
v3
Comments
Sergey Alexandrovich Kryukov 1-Jul-11 0:36am    
This is not a question. You don't explain what exactly is your problem. Look MSDN help pages on each method or type used here, that's it.
--SA
walterhevedeich 1-Jul-11 1:02am    
You might also want to post your question on the article. The author knows better about the code than anyone else.
bagus bujangga 1-Jul-11 4:42am    
this is my question, I am write in code above.
here I copy this question,
Here I want to get by pixel(x,y) position simultaneously with other pixel value for exp:
(x,y) is position of the pixel.

result= pixel(0,0)+pixel(1,0)+pixel(2,0)+pixel(0,1)+pixel(1,1)+
pixel(2,1)+pixel(0,2)+pixel(1,2)+ pixel(2,2)
result=result/9;

How to do it?

and I don't understand with "p += nOffset;", I try disable it and still running. can you explain to me?

1 solution

Don't post this under Questions & Answers - if you got the code from an article, then there is a "new message" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.

Mind you, I don't think you will get much help with a generic "explain this to me" - you might be better off runnin gthis through teh debugger and watching what happens - it is pretty obvious, after all.
 
Share this answer
 

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