Click here to Skip to main content
16,011,647 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{
        if (pictureBox1.Image != null) 
        { 
 
 
          Bitmap b = new Bitmap(pictureBox1.Image); 
          if (e.X >= 0 && e.X <= b.Width && e.Y >= 0 && e.Y <= b.Height) 
          { 
              Color c = b.GetPixel(e.X, e.Y); 
              mouseCOL.Text = "R : " + c.R.ToString() + " G : " + c.G.ToString() 
                          + " B : " + c.B.ToString(); 
          } 
 
 
            /*Bitmap b = new Bitmap(pictureBox1.Image); 
            Color c = b.GetPixel(e.X, e.Y); 
 
            mouseCOL.Text = "R : " + c.R.ToString() + " G : " + c.G.ToString() 
            + " B : " + c.B.ToString();*/ 
 
 
        } 
        else 
        { 
 
        } 
    } 


eX, eYcorresponding values ​​for Width of the crew than the size of the image seems to occur when you just can not figure out how to handle exceptions Mouse Move event put him in a Picture Box

Go to any part of the parameter must be positive and will be less than Width.

The parameter name: x

Tteuneyo that errors

pictuerBox size and ready to bring us a picture to zoom Style
Posted
Updated 19-Dec-11 22:11pm
v2

1 solution

Please, please, do not do that!
Every time you move the mouse (and that can be a hundred or more times a second) you are creating a new bitmap the same as the existing one! And you don't dispose of it, so it is hanging around until the Garbage Collector comes along to get rid of it.

I cannot think of a single less efficient way to do that!

Fiurstly, if you create a bitmap, you are responbsible for Disposing it, either explicitly, or by creating it in a using block:
C#
using(Bitmap b = new Bitmap(pictureBox1.Image))
{
   if (e.X >= 0 && e.X <= b.Width && e.Y >= 0 && e.Y <= b.Height)
   {
       Color c = b.GetPixel(e.X, e.Y);
       mouseCOL.Text = "R : " + c.R.ToString() + " G : " + c.G.ToString()
                   + " B : " + c.B.ToString();
   }


     /*Bitmap b = new Bitmap(pictureBox1.Image);
     Color c = b.GetPixel(e.X, e.Y);

     mouseCOL.Text = "R : " + c.R.ToString() + " G : " + c.G.ToString()
     + " B : " + c.B.ToString();*/
 }
But a better idea is not to create a new one at all:
C#
Bitmap b = (Bitmap) pictureBox1.Image;
if (e.X >= 0 && e.X <= b.Width && e.Y >= 0 && e.Y <= b.Height)
{
    Color c = b.GetPixel(e.X, e.Y);
    mouseCOL.Text = "R : " + c.R.ToString() + " G : " + c.G.ToString()
                + " B : " + c.B.ToString();
}


  /*Bitmap b = new Bitmap(pictureBox1.Image);
  Color c = b.GetPixel(e.X, e.Y);

  mouseCOL.Text = "R : " + c.R.ToString() + " G : " + c.G.ToString()
  + " B : " + c.B.ToString();*/


The rest of your problem I don't understand at all - it's probably a language thing, but please try top give us the actual error message, and indicate on which line you are getting it.
 
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