How to get pixel value at mouse position

Started by paul kennedy, December 05, 2021, 06:11:32 AM

Previous topic - Next topic

paul kennedy

Hi
ImageBox is a lovely control!
I am trying to get and set of the pixel at the current mouse location
from your sample code I can see we can retrieve teh pixel coordinates no problem with


    private void UpdateCursorPosition(Point location)
    {
      if (!fitCursorLocationToBoundsToolStripMenuItem.Checked || imageBox.IsPointInImage(location))
      {
        Point point;

        point = imageBox.PointToImage(location);
        cursorToolStripStatusLabel.Text = this.FormatPoint(point); 
        // Q1 how can I GET the pixel value (ie the R,G,B value)       
       
        // Q2 how can I SET the pixel value (ie the R,G,B value)
    }



many thanks for such a great control!

Richard Moss

Hello,

Assuming your backing image is a Bitmap rather than a more generic Image, you can use the GetPixel method to get the colour of the pixel at the given location.

There is also a corresponding SetPixel method you can use to set the pixel. However, this method is chronically slow so if you need to update many pixels you're better off using something else. If you don't want to directly deal with unsafe code then have a look at one of the FastBitmap implementations.

Also note that if you do change the underlying bitmap instance via these methods you will need to tell the ImageBox control to repaint itself via the Invalidate or Refresh methods.

Regards;
Richard Moss
Read "Before You Post" before posting (https://forums.cyotek.com/cyotek-webcopy/before-you-post/). Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.

paul kennedy

Hi Richard
thanks for getting back to me.  I read your feedback with interest and have been trying to use the SetPixel method to modify the ImageBox.Image.  unfortunately I could not compile this...

          //this does not compile...
          //imageBox.Image.SetPixel(point.X, point.Y, Color.White);

I thien attempted to cast the Image to a Bitmap and modify this.  I think this actually works, but it looks like it takes a copy so your imageboax viewer does not reflect the change in pixel color. Hmm.

here is the code I put together.  I think I am close, just stuck right now!

Our goal is to permit the user to modify a pixel by clicking on it.  this is part of a machine learning project, in which we need to create training datasets.  we are using ImageBox to create the labels for the training library.




    private void imageBox_MouseClick(object sender, MouseEventArgs e)
    {
      //if we get a right click lets edit the pixel.  it is either on or off so toggle
      if (e.Button == MouseButtons.Right)
      {
        //MessageBox.Show(e.ToString());

        if (!fitCursorLocationToBoundsToolStripMenuItem.Checked || imageBox.IsPointInImage(e.Location))
        {
          Point point;
          point = imageBox.PointToImage(e.Location);

          //this does not compile...
          //imageBox.Image.SetPixel(point.X, point.Y, Color.White);

          //I can cast the image to a bitmap and then change the pixel color but the change is no visualised in imageBox
          imagebitmap = new Bitmap(imageBox.Image);
          imagebitmap.SetPixel(point.X, point.Y, Color.Black);
          imageBox.Invalidate();

        }


regards
pk