Help with drawing in picturebox...

lidds

Junior Contributor
Joined
Nov 9, 2004
Messages
210
I have a picturebox called picWindow and I am currently assigning a image to the picturebox, which works fine. The problem I have is that I need to be able to add lines, boxes etc. to the image, and preferrably be able to identify the entities (lines etc.) so that they can either be modified or deleted without having to erase (rub-out) the lines etc. and the image on which they have been placed. Has anyone got an example in vb.net or a link that covers this??

This is the code that I am using to assign the image to the picturebox:
Code:
Dim path As String = "c:\temp\pic\image.bmp"
Me.picWindow.SizeMode = PictureBoxSizeMode.AutoSize
Me.picWindow.Image = Image.FromFile(path)
Dim g As Graphics
g = Me.picWindow.CreateGraphics

Cheers

Simon
 
lidds:
Below is a simplified example of an approach I have used before to accomplish a similar task. The gist of it is that you maintain a collection with the drawing information for different items to be drawn on your Control. You can then add and remove items to the collection to add and remove them from the Control. This approach can be significantly enhanced to meet your specific needs......I would suggest creating a custom PictureBox which encapsulates all of the functionality. Hope this helps.

Code:
// Collection to hold current items to draw
private ArrayList itemsToDraw = new ArrayList();

// Type to hold item drawing information
private struct DrawingItem
{
	public int DrawVal1;
	public int DrawVal2;
	public int DrawVal3;
	public int DrawVal4;
	public Color DrawColor;
	public DrawingItemType Type;


		public DrawingItem(int val1, int val2, int val3, int val4, Color color, DrawingItemType drawType)
		{
			this.DrawVal1 = val1;
			this.DrawVal2 = val2;
			this.DrawVal3 = val3;
			this.DrawVal4 = val4;
			this.DrawColor = color;
			this.Type = drawType;
		}
}

// Type to define drawing item
private enum DrawingItemType
{
	Line,
	Rectangle,
	Ellipse
}

// Add drawing item to collection
private void AddItem(DrawingItem item)
{
                  this.itemsToDraw.Add(item);
                  this.pictureBox1.Invalidate();
}

// Remove drawing item from collection
private void RemoveItem(DrawingItem item)
{
             this.itemsToDraw.Remove(item);
             this.pictureBox1.Invalidate();
}
		
// Draw all current drawing items when paint is called
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	if(this.itemsToDraw.Count > 0)
	{
		for(int count = 0; count < this.itemsToDraw.Count; count++)
		{
			DrawingItem item = (DrawingItem) this.itemsToDraw[count];
			switch(item.Type)
			{
				case DrawingItemType.Ellipse:
					e.Graphics.DrawEllipse(new Pen(item.DrawColor), item.DrawVal1, item.DrawVal2, item.DrawVal3, item.DrawVal4);
				break;

				case DrawingItemType.Line:
					e.Graphics.DrawLine(new Pen(item.DrawColor), item.DrawVal1, item.DrawVal2, item.DrawVal3, item.DrawVal4);
				break;

				case DrawingItemType.Rectangle:
					e.Graphics.DrawRectangle(new Pen(item.DrawColor), item.DrawVal1, item.DrawVal2, item.DrawVal3, item.DrawVal4);
				break;
			}
		}
	}
}
 
Back
Top