SetPixel method in C#

May 11 2010 6:13 AM
Hello to everyone,

I'm writing code for an application form with a pictureBox inside it.
All i want to do is, to load an image (in a picture box)  and after that to change pixels in this image.
After searching, I've already read that SetPixel Method-in C#-can help me with that!
In PaintEvent of pictureBox the code is:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // Create a Bitmap object from a file.
            Bitmap myBitmap = new Bitmap("D:\\...\\SP_A0195.jpg");
            // Draw myBitmap to the screen.
            e.Graphics.DrawImage(myBitmap,0,0,myBitmap.Width,myBitmap.Height);
            // Set each pixel in myBitmap to red.
            for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
            {
                for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
                {
                    myBitmap.SetPixel(Xcount, Ycount, Color.Red);
                }
            }
            // Draw myBitmap to the screen again.
            e.Graphics.DrawImage(myBitmap,myBitmap.Width,0,myBitmap.Width,myBitmap.Height);

        }

The code above doesn't work but i can not understand why...please help on how to do that.

Thank you in advance for your time!

Answers (11)