Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need to add either circle or rectangle on an image with a mouse click. later when i press delete button and click on any circle or rectangle, only that particular circle/rectangle should be deleted.
Am using pictureBox and windows Form.
Kindly help.
Posted
Comments
Sandeep Mewara 16-May-11 4:25am    
Tried anything?
Deepurs 16-May-11 4:28am    
i can draw rectangle on a mouse click. But not able to delete...

The circle rectangle all those graphics objects will be destroyed after the scope of the function you are drawing it. Now it is only the part of the bitmap pixels. It won't offer you a click event.

But WPF these graphics objects live and have a click event. However it is not impossible on a windows forms. You can use the simple control panel. Draw your graphics on a panel, make the background transparent. Place the panel on the image. Now it responds to a click and when click you can self destroy the panel. A helpful example here.

Clickable areas in a picturebox[^]

This use the graphic transparent, but you can draw a visible graphic on the panel.

Good luck
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-May-11 5:19am    
My 5.
--SA
Albin Abel 16-May-11 6:14am    
Thanks SAKryukov. Yet to browse your answers with interesting facts. :)
Deepurs 16-May-11 5:21am    
i need to draw rectangle on image.
Albin Abel 16-May-11 6:19am    
You need to tell me display or draw. Drawn can't be removed easily, unless using intensive image processing algorithms. My solution offers you to display graphics over the image, which responds clicks + other events. Whatever graphics you drawn on the panel or removed. Let it sync with a Graphic data in a Dictionary. Once you finalize the graphics (probably a save method) you can draw those graphics to the image permanently. Is this helpful?
Vikram_ 16-May-11 5:30am    
Yeah same logic defining regions for control. But better and efficient option or alternative.
Here is my Code... i tried something like this... on delete button(button2), drawing the rectangle in black color. I know its not correct, but just trying something.please help. its very urgent.
please frds... help me.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace addRect
{
public partial class Form1 : Form
{
Image newImage = null;
bool btn1 = false;
bool btn2 = false;
Rectangle rect1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newImage = Image.FromFile("C:\\Documents and Settings\\ing12675\\Desktop\\images\\ectopic-ruptured-1a.jpg");
pictureBox1.Image = newImage;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
//int x = e.X;
//int y = e.Y;
if (btn1)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
rect1 = new Rectangle(e.X, e.Y, 20, 20);
g.DrawRectangle(Pens.Red, rect1);
btn1 = false;
}
}
else if (btn2)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
if (rect1.Contains(new Point(e.X, e.Y)))
{
g.DrawRectangle(Pens.Black, rect1);
btn2 = false;
}
}
}
pictureBox1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
btn1 = true;
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
}
private void button2_Click(object sender, EventArgs e)
{
btn2 = true;
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
}
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(@"D:\\test.jpg");
}

}
}
 
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