Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,


I am trying to define polygon over Image Dynamically....i.e. User will define polygon points on mouse click and after that defined area's color will change..

please please help me ...

Thanks in advance
Posted
Updated 27-Mar-12 22:02pm
v2

Create a class level GraphicsPath object:
C#
GraphicsPath path = new GraphicsPath();

In your mouse Down event, add lines to the path:
C#
path.AddLine(oldPoint, newPoint);
Where 0old point is the previous mouse click location.
Handle the Paint event for whatever object holds your image, and construct a Region from the GraphicsPath. You can then draw as needed within the region:
C#
Region r = new Region(path);
e.Graphics.FillRegion(Brushes.Red, r);
 
Share this answer
 
Comments
ProEnggSoft 28-Mar-12 5:41am    
To the point. +5
rahulbhadouria 28-Mar-12 6:11am    
I am still unable to do this ...because I am new to "system.drawing" .So please tell me some details so that I can do this.

Thanks a lot for helping...

Thanks
OriginalGriff 28-Mar-12 6:18am    
Which bit do you need more details for?
rahulbhadouria 28-Mar-12 8:16am    
I want that user will select polygon and the vertices of selected polygon are stored either in DB or any where.
OriginalGriff 28-Mar-12 9:48am    
So instead of just adding them to the GraphicsPath, add them to your store.
Hi,
drawing is the basic programming for C#;

First create a sample of windows form, and drop the picture box on that.

and replace the code to application

* this is for total form class.

* and add the required events (pictureBox1_MouseClick).


****************************************************************
public partial class Form1 : Form
{
int iNumberofClicks = 0;
Point[] pointArray = new Point[100];
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pointArray[iNumberofClicks].X = e.X;
pointArray[iNumberofClicks].Y = e.Y;
Pen PenSpikes = new Pen(Color.Green);
SolidBrush solidBrush = new SolidBrush(Color.Blue);
iNumberofClicks++;
if (iNumberofClicks > 1)
{
Point[] CurrentpointArray = new Point[iNumberofClicks];

for (int i = 0; i < iNumberofClicks; i++)
{
CurrentpointArray[i].X = pointArray[i].X;
CurrentpointArray[i].Y = pointArray[i].Y;
}

Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics offScreenDC = Graphics.FromImage(canvas);
// offScreenDC.DrawPolygon(PenSpikes, CurrentpointArray);
offScreenDC.FillPolygon(solidBrush, CurrentpointArray);
pictureBox1.Image = canvas;
offScreenDC.Dispose();
}
}
}

**********************************************************
Regards

Vinay
 
Share this answer
 
Comments
rahulbhadouria 28-Mar-12 8:14am    
Thank you so much Vinay,,,,

But if I am using Image as picture box background...It disappears after two clicks ...I want to do same thing over Image and want to store the selected Vertices.

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