Reputation: 2125
I'm trying to make a little 15x15 playing grid, and drawing the solid outline rectangles side-by-side is working just fine, however when I try to call FillRectangle() to fill the aforementioned cells the results are .. Not quite what I expected.. I think it's easier if I just show you..
Here's the code
//Draw grid now since InitGrid() is done
using (Graphics formGraphics = this.CreateGraphics())
{
Pen pen = new Pen(Color.Black);
SolidBrush brush = new SolidBrush(Color.Beige);
int y = new int();
List<Rectangle> rect = new List<Rectangle>();
foreach (Cell cell in pGrid.cells)
{
rect.Add(new Rectangle(cell.Point.X, cell.Point.Y, 18, 18));
formGraphics.DrawRectangle(pen, rect[y]);
formGraphics.FillRectangle(brush, rect[y++]);
}
}
Also an extra question, since I am using a using
code block do I still have to call Dispose()
on the Pen
and Graphics
?
Upvotes: 1
Views: 1653
Reputation: 26446
The differences in the "covered area" are caused by DrawRectangle
using a Pen
which has a thickness of 1px, and 0.5px of that is added to the outer edges of the rectangle, and then rounded "to the bottom-right". These bottom-right borders are then overwritten by the next cell's FillRectangle
.
You could use a border thickness of 2, which would cause the border to be 2 pixels larger than the filled area, and thus appear 1px thick on each side.
Pen pen = new Pen(Color.Black, 2);
This only works if there is actual space between the cells to show the borders in. If there is no room for the borders you'll have to either shrink your cell size or grow the grid itself.
Another solution (since you mention the outlines are fine) is to first call FillRectangle
and then DrawRectangle
.
Also a using
around the pen and brush won't hurt. Note that you can also use the static Pens.Black
(unless you're going for the solution of a 2px thick border) and Brushes.Beige
.
Upvotes: 3