# Thread: [RESOLVED] tiling problem with picturebox

1. ## [RESOLVED] tiling problem with picturebox

I'm stuck on a basic math problem

This is the code that I'm using

Code:
```    Private Sub picMap_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picMap.Paint
e.Graphics.DrawRectangle(Pens.Red, cursorPos.X * 48, cursorPos.Y * 48, 48, 48)
End Sub```
This works almost as intended. If cursorPos.X = 0 then the rectangle gets placed at x coordinate 0 in the picturebox. Same with y.

The problem is, that if let's say I have 29, which is the last square in the picturebox that is 864 in width (30 tiles of 48x48), then the rectangle is missing it's right side. In other words it's 1 pixel past the picturebox. on the right. On the left at 0 it's perfect. I can't figure out how to adjust it. If I do -1 pixel then the left side at 0 is cut off by 1 pixel.

I was coding for too long, I burned out. Can someone fix this for me please?

2. ## Re: tiling problem with picturebox

I managed to get through this. Here is how I solved it. Y has to be done the same way.

Code:
`e.Graphics.DrawRectangle(Pens.Red, Math.Max(cursorPos.X * 48, 1) - 1, cursorPos.Y * 48, 48, 48)`

3. ## Re: [RESOLVED] tiling problem with picturebox

A 48 pixel wide rectangle will technically be 49 pixels wide, if you include both left and right sides. The left size is at pixel 0, and the right size is at pixel 48.
That way, if you draw a second rectangle next to the previous, with the left side at 48, the left side of the second coincides with the right side of the first, so you end up with a clean grid (otherwise your lines would look double wide where the rectangles meet).
If you're not drawing a grid, you could just make the rectangle a pixel shorter, (w,h = 47,47).
If you're drawing a grid, you could make the picturebox 1 pixel larger in height and width, to accomodate the right and bottom line.
If you use FillRectangle, then the Fill will not fill out to the 48th pixel, but stop at 47 (0 to 47 so 48 pixels wide), because you don't want the fill edges to overlap the next fill.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured