-1

I have image 10*10 mult it in factor and loading the image to picturebox I have problem to select the mouse position when I clicking in everywhere over the image the point showing in the corner Upper-left what is the error??

lena
  • 730
  • 2
  • 11
  • 23
  • have you tried doing a google search on the following `PointToClient(Cursor.Position)` http://stackoverflow.com/questions/8201286/get-cursor-position-with-respect-to-the-control-c-sharp also I would think that you would want to do this on the Mouse Click event hot the Image Click Event – MethodMan Feb 15 '15 at 20:11
  • I'm using pictureBox click event (pb1_Click) – lena Feb 15 '15 at 20:14
  • Us the `MouseClick` event instead. Its params have the coordinates (relative to the `PB.ClientSize`) – TaW Feb 15 '15 at 21:08
  • @TaW I'm using MouseClick but the same problem ! – lena Feb 16 '15 at 19:14

1 Answers1

0

To draw into a zoomed Image of a PictureBox this works fine for me:

float zoom = 7.5f;

public Form1()
{
    InitializeComponent();
    pb1.SizeMode = PictureBoxSizeMode.Zoom;
    pb1.ClientSize = new Size((int) (pb1.Image.Size.Width  * zoom), 
                              (int) (pb1.Image.Size.Height * zoom) );
}



private void pb1_MouseClick(object sender, MouseEventArgs e)
{
   int x = (int)Math.Round(e.X / zoom) ;
   int y = (int)Math.Round(e.Y / zoom) ;
   Bitmap bmp = (Bitmap) pb1.Image;
   bmp.SetPixel(x, y, Color.Red);
   pb1.Refresh();
}

Note: This works equally well if the PictureBox is sitting in an AutoScroll Panel.

enter image description hereenter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
  • thank for answer ,please if you can see my update ,I have this error NullReferenceexception... – lena Feb 16 '15 at 20:40
  • Where do you get the exception? The code assumes the pb1.Image is loaded (from the start.) I don't see the zoom variable, but it wouldn't throw an exception but a compiler error.. – TaW Feb 16 '15 at 20:44
  • zoom variable I use it in my code but here not :( but the same error NullReferenceexception.... – lena Feb 16 '15 at 20:52
  • Where?????????? Note: you can set the `pb1.ClientSize` only __after__ setting the `Image` !! (Also note the improved calculation!) – TaW Feb 16 '15 at 20:55
  • here and same the error pb1.ClientSize = new Size((int) (pb1.Image.Size.Width * zoom), (int) (pb1.Image.Size.Height * zoom) ); ,I'm work on the code in question – lena Feb 16 '15 at 21:18
  • I thought so. Make sure the size is __always and only__ set __after__ the Image is set! – TaW Feb 16 '15 at 21:19
  • The solution is very simple: __Move__ the line in question to the spot in the `button1_Click` I have marked as `//***` - My test program sets the image in the designer; I should have noticed that you use a dialog to set it. Sorry! – TaW Feb 16 '15 at 21:29