Click here to Skip to main content
15,664,619 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
in this code I load image to picturebox and by mouse click event I want to select 2 point and give each one different color my problem is the pictureBox Mouse Click not respond
Updated 7-Feb-15 7:15am

1 solution

Don't PictureBox and, in most cases, don't use SetPixel. First one does not help you, only adds hassles and takes some extra resources and your development time, second one is prohibitively slow (but file if you want just to set one of few pixels). I'll tell you want to do.

I explained why not using PictureBox and what to do instead in my past answers:
Append a picture within picturebox[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].

And the problem with mouse position will be solved automatically: your mouse event argument instance (passed to your event handler) will give you the mouse position in the control's coordinate, as well as your graphics.

For further detail in image rendering, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
see also: How to speed up my application?[^],
Zoom image in C# .net mouse wheel[^].

You can actually use SetPixel, but only if you want to set one or just few, but this is almost never needed in practice. In all other cases, for reasonable performance, you need to use LockBits:[^].

You will find a code sample here:[^].

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