Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Am trying to draw a small circle on the image with a mouse click. But the circle is drawn away from the mouse clicked point. here is my code.
C#
private void pictureBox1_MouseClick(object sender, MouseEventArgs e1)
        {
            string str = "O";
            if ((e1.Button & MouseButtons.Left) == MouseButtons.Left)
            {
                Bitmap b = (Bitmap)pictureBox1.Image;
                pictureBox1.Image = b;
                Graphics g = Graphics.FromImage(b);
                p = new Point(e1.X, e1.Y);
                Pen p3 = new Pen(Color.Red, 1);
                this.Text = p.ToString();
                SizeF sizef = g.MeasureString(str, Font);
                f = new Font(new FontFamily("Times New Roman"), 10);
                g.DrawString(str, f, Brushes.Red, ((p.X)), ((p.Y)));
            }
            pictureBox1.Invalidate();
        }
Posted
Updated 11-May-11 0:31am
v2
Comments
yesotaso 11-May-11 7:21am    
The method you use depends on default Font type-size etc. Think it lettor "O" as a little picture this method will align left side of picture to the coordinates you enter. Even if you calculate that and normalize it it wouldnt be the same with another font(another machine-pc etc). You should consider below solutions.
Looking at code: what did you do with "sizef". And is the size of picturebox equal to size of original picture? (ie. did you strech it?)
Deepurs 12-May-11 0:56am    
yes am streching the imageto fit the pictureBox size...

You have to measure your string like you already did, but then you have to paint your string at the mouseclick's coordinates minus half the string's width to the left and minus half its height to the top, if you want to have the string centered to your mouseclick.

Example:

You click on x/y 100/200 and your string's width and height is 20 pixels. To center the string on the mouseclick's coordinates you have paint it to the location x:90 and y:190


But anyway, as the others told you already, I would prefer to paint a circle or the cross/X myself.


modified:

Try this:
f = new Font(new FontFamily("Times New Roman"), 10);
SizeF sizef = g.MeasureString(str, f);
g.DrawString(str, f, Brushes.Red, ((p.X-sizeF.widtdh/2)), ((p.Y-sizeF.heigth/2)));
 
Share this answer
 
v3
Comments
Deepurs 11-May-11 7:14am    
still my circles are drawn much away from the clicked point :(
Smithers-Jones 11-May-11 7:57am    
I modified my answer. Still it would be better to draw the cirles with DrawEllipes, this way you have far more control.
Are you actually drawing an "O" string to get a circle?

Why don't you use g.DrawEllipse()?
 
Share this answer
 
Comments
Deepurs 11-May-11 6:31am    
Sometimes I need to draw Cross i.e., X also. So in general, i change the 'str' value according to the requirement.
Hi there,

You want the center of the circle to the point where you clicked on the picture box right? In that case, using fonts is going to complicate your problem, since you do not know the height and width of a the character string you are going to draw.

There are indeed ways of calculating the font size (height and width in pixels) but it's easier to draw a circle or a cross. You say drawing an "X" is hard, but actually it's not, all you have to do is draw 2 lines with swapped co-ordinates.

Give it a thought and let us know what you choose to go forward with, then we might be able to work our way towards a code sample.

Hope this helps :) Regards
 
Share this answer
 
Comments
Deepurs 11-May-11 6:55am    
I have already tried with the character. But could get the exact output. Please guide me in the other way i.e., draw circle and 2 lines with swapped co ordinates.
Smithers-Jones 11-May-11 7:05am    
Hi Pasan,

OP knows the width and height already since he measures them in his code-example. Just the positioning seems to be a problem. See my solution please.
CodeHawkz 11-May-11 9:34am    
:) Agreed.
Specially for the one who cannot understand things in first place:
Warning! I see no indication that you need to draw on image.

Do not use it!

—SA
 
Share this answer
 
Warning! I see no indication that you need to draw on image.

Chances are, you need to draw immediately in your control (a Panel or a Form, for example). You need to handle the event Paint or override the method OnPaint, use the instance of Graphics supplied in event arguments to draw our your data. When your data is modified, call Control.Invalidate.

Even if you need to save a bitmap, you should not use the bitmap for screen presentation. Instead, abstract out the rendering method like this: void Render(System.Drawing.Graphics graphics) and call this method from Paint or OnPaint. When you need to save your current graphics in the bitmap (file or something), create a bitmap and point on it using your Render method.

—SA
 
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