Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Image coordinates within a picturebox

Image coordinates within a picturebox

Scheduled Pinned Locked Moved C#
question
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Quake2Player
    wrote on last edited by
    #1

    How can I know the coordinates of the image within the picturebox when clicked? Subject to: 1. The picturebox mode is Zoom 2. The picturebox can have any size (but the image inside is uniformely scaled because of Zoom mode) I would appreciate ideas for solving this, Thanks

    Q K 2 Replies Last reply
    0
    • Q Quake2Player

      How can I know the coordinates of the image within the picturebox when clicked? Subject to: 1. The picturebox mode is Zoom 2. The picturebox can have any size (but the image inside is uniformely scaled because of Zoom mode) I would appreciate ideas for solving this, Thanks

      Q Offline
      Q Offline
      Quake2Player
      wrote on last edited by
      #2

      Forget about it, I found an extended PictureBox: Mouse Position over Image in a PictureBox[^]

      1 Reply Last reply
      0
      • Q Quake2Player

        How can I know the coordinates of the image within the picturebox when clicked? Subject to: 1. The picturebox mode is Zoom 2. The picturebox can have any size (but the image inside is uniformely scaled because of Zoom mode) I would appreciate ideas for solving this, Thanks

        K Offline
        K Offline
        krishy19
        wrote on last edited by
        #3

        Did you find an answer to this? If so, can you please share? I need to do exactly the same. Thanks.

        Q 2 Replies Last reply
        0
        • K krishy19

          Did you find an answer to this? If so, can you please share? I need to do exactly the same. Thanks.

          Q Offline
          Q Offline
          Quake2Player
          wrote on last edited by
          #4

          Whats wrong with the one I posted?

          1 Reply Last reply
          0
          • K krishy19

            Did you find an answer to this? If so, can you please share? I need to do exactly the same. Thanks.

            Q Offline
            Q Offline
            Quake2Player
            wrote on last edited by
            #5

            The thing is that the PictureBoxExtended brings only one event (MouseMoveOverImage), and I needed to create another (MouseClickedImage), but its really easy: This is what it brings:

            	/// Handler for when the mouse moves over the image part of the picture box
            	public delegate void MouseMoveOverImageHandler(object sender, MouseEventArgs e);
            
            	/// Occurs when the mouse have moved over the image part of a picture box
            	public event MouseMoveOverImageHandler MouseMoveOverImage;
            

            And I added the following

            	/// Handler for when the mouse **clicks** over the image part of the picture box
            	public delegate void MouseClickedImageHandler(object sender, MouseEventArgs e);
            
            	/// Occurs when the mouse have **clicked** over the image part of a picture box
            	public event MouseMoveOverImageHandler MouseClickedImage;
            

            And then go down to the protected methods part and create a method that overrides the OnMouseClick method of the base class PictureBox, so:

                protected override void OnMouseClick(MouseEventArgs e)
                {
                    **base.OnMouseClick(e);**
                    if (Image != null)
                    {
                        if (MouseClickedImage != null)
                        {
                            Point p = TranslatePointToImageCoordinates(e.Location);
                            if (p.X >= 0 && p.X < Image.Width && p.Y >= 0 && p.Y < Image.Height)
                            {
                                MouseEventArgs ne = new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta);
                                MouseClickedImage(this, ne);
                            }
                        }
                    }
                }
            

            And what I wrote here is just like the code within the OnMouseMove method... so I didn't had to think anything... Look that i'm calling to the base method also, so you can still use the MouseClick event normally Cya!

            K 1 Reply Last reply
            0
            • Q Quake2Player

              The thing is that the PictureBoxExtended brings only one event (MouseMoveOverImage), and I needed to create another (MouseClickedImage), but its really easy: This is what it brings:

              	/// Handler for when the mouse moves over the image part of the picture box
              	public delegate void MouseMoveOverImageHandler(object sender, MouseEventArgs e);
              
              	/// Occurs when the mouse have moved over the image part of a picture box
              	public event MouseMoveOverImageHandler MouseMoveOverImage;
              

              And I added the following

              	/// Handler for when the mouse **clicks** over the image part of the picture box
              	public delegate void MouseClickedImageHandler(object sender, MouseEventArgs e);
              
              	/// Occurs when the mouse have **clicked** over the image part of a picture box
              	public event MouseMoveOverImageHandler MouseClickedImage;
              

              And then go down to the protected methods part and create a method that overrides the OnMouseClick method of the base class PictureBox, so:

                  protected override void OnMouseClick(MouseEventArgs e)
                  {
                      **base.OnMouseClick(e);**
                      if (Image != null)
                      {
                          if (MouseClickedImage != null)
                          {
                              Point p = TranslatePointToImageCoordinates(e.Location);
                              if (p.X >= 0 && p.X < Image.Width && p.Y >= 0 && p.Y < Image.Height)
                              {
                                  MouseEventArgs ne = new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta);
                                  MouseClickedImage(this, ne);
                              }
                          }
                      }
                  }
              

              And what I wrote here is just like the code within the OnMouseMove method... so I didn't had to think anything... Look that i'm calling to the base method also, so you can still use the MouseClick event normally Cya!

              K Offline
              K Offline
              krishy19
              wrote on last edited by
              #6

              Thanks for your help! :) Appreciate it.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups