Click here to Skip to main content
16,016,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to draw symbols like 'O' and 'X' just with a mouse click on the stretched image. Am using PictureBox and windows form.
i tried something like this.
private void pictureBox1_MouseClick(object sender, MouseEventArgs e1)
        {
            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, 2);
                this.Text = p.ToString();
                g.DrawEllipse(p3, p.X, p.Y, 15, 15);
                pictureBox1.Invalidate();
            } 
}

Problem is the circle is drawn away from the mouse clicked point. I think its because of image stretch. Please help.
Posted
Updated 11-May-11 22:13pm
v2

1 solution

1) You need to declare "p":
Point p = new Point(e1.X, e1.Y);

2) You need to dispose of all the Graphics, and Pen objects you create - not doing that will cause problems later.
3) The start point for a Ellipse is always the top left corner: Try offseting them:
g.DrawEllipse(p3, p.X - 7, p.Y - 7, 15, 15);


From OP:
"pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;"



Now, that is relevant information!
When you use PictureBoxSizeMode.StretchImage you apply a transform too all graphics operations, to make them scale appropriatly into the re-sized image. You don't want that!

Instead use this:
//pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
using (Image source = Image.FromFile(@"F:\Temp\BTC.jpg"))
    {
    Image resized = new Bitmap(source, pictureBox1.Size);
    pictureBox1.Image = resized;
    }


And modify your Mouse Click event as per the suggestions above:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
        Bitmap b = (Bitmap) pictureBox1.Image;
        pictureBox1.Image = b;
        using (Graphics g = Graphics.FromImage(b))
            {
            Point p = new Point(e.X, e.Y);
            using (Pen p3 = new Pen(Color.Red, 2))
                {
                this.Text = p.ToString();
                g.DrawEllipse(p3, p.X - 7, p.Y - 7, 15, 15);
                }
            }
        pictureBox1.Invalidate();
        }
    }
Seriously, not disposing of objects (particularly graphics related objects) causes unpredictable problems later on - it can be very hard to work out what the problem is if it doesn't occur until two weeks after you wrote and tested this code!
 
Share this answer
 
v2
Comments
Deepurs 12-May-11 4:17am    
still not working... :( someone plz guide me
OriginalGriff 12-May-11 4:20am    
Copy and paste exactly the code you are using for the pictureBox1_MouseClick.
When I try the code here, with my suggestions, it works fine.
Deepurs 12-May-11 4:30am    
Am stretching the image to the pictureBox size.
OriginalGriff 12-May-11 4:35am    
How?
Deepurs 12-May-11 4:38am    
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

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