-
April 26th, 2012, 09:54 PM
#1
Creating Drawing Grids
Hi all
I'm using Visual Studio 2005 C# and was wondering how to make a grid that can be turned visible and invisible. I have no problem creating the grid using graphics and buffers and all that, that's all done. I'm having a hard time being able to remove the grid afterwards. Essentially what I am doing is trying to create a grid that the user can activate and deactivate to help guide their drawing.
So far what I am doing right now is I have two buffers. The one that is continuously updated with mouseclick drawing is placed into the griddrawing buffer. This grid buffer then takes the image and the drawings on it and creates a grid. However while the grid is showing you can't see any drawing changes on the image because I've switched buffers.
I'm wondering if there is an easier way to create a toggleable grid or if I'm headed in the right direction, or if I'm completely off base.
thanks
Code:
private void Form1_Load(object sender, EventArgs e)
{
//buffer = new Bitmap(background,this.Width, this.Height);
tileBuffer = new Bitmap(background, this.Width, this.Height);
tileLayer = Graphics.FromImage(tileBuffer);
this.BackgroundImage = tileBuffer ;
this.BackgroundImageLayout = ImageLayout.Stretch;
Origin = new Point(0, 0);
GridCellSize = new Size(10, 10);
HorizontalCells = 10;
VerticalCells = 10;
}
private void DrawGrid()
{
Point startP = new Point();
Pen pencil = new Pen(Color.Blue, 3.0f);
Point endP = new Point();
buffer = new Bitmap(tileBuffer , this.Width, this.Height);
gridLayer = Graphics.FromImage(buffer);
// Draw horizontals
startP.X = Origin.X;
endP.X = Origin.X + VerticalCells * GridCellSize.Width;
for (int i = 0; i <= HorizontalCells; i++)
{
startP.Y = Origin.Y + i * GridCellSize.Height;
endP.Y = startP.Y;
gridLayer.DrawLine(pencil, startP, endP);
}
// Draw verticals
startP.Y = Origin.Y;
endP.Y = Origin.Y + HorizontalCells * GridCellSize.Height;
for (int i = 0; i <= VerticalCells; i++)
{
startP.X = Origin.X + i * GridCellSize.Width;
endP.X = startP.X;
gridLayer.DrawLine(pencil, startP, endP);
}
gridLayer.Dispose();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// DrawGrid();
}
private void button1_Click(object sender, EventArgs e)
{
if (vis == false)
{
DrawGrid();
this.BackgroundImage = buffer;
vis = true;
}
else
{
this.BackgroundImage = tileBuffer ;
vis = false;
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Pen pen = new Pen(Color.Red);
Point loc = new Point (e.X +30, e.Y);
tileLayer.DrawLine(pen, e.Location,loc );
this.Invalidate();
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|