Hi,
I am trying to do a simple tile-based game in Visual Basic 2005. In VB6 I allways used BitBlt, but in this thread:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1782615&SiteID=1
I was advised to use the drawimage function instead. I use two pictureboxes, one with the set of tiles, and one that is visible in the game. My problem is, using the drawimage, is that when I put the second tile in the destination picturebox, the first tile disappears. Also, If I at designtime put an image in the picturebox, It also disappears when I put in the first tile.
How can I keep the "backgroundpicture" and the previous tile when adding the additional tiles?  | | Samus Thursday, June 28, 2007 11:33 AM |
I think I got the solution, the new image in my code is only to be called the first time. This works for me:
Code Snippet
Dim x, y As Byte
PboxPlayarea.Image = New Bitmap(180, 180)
Dim G As Graphics = Graphics.FromImage(PboxPlayarea.Image)
For y = 0 To 10
For x = 0 To 10
G.DrawImage(PBoxCopy.Image, x * 18, y * 18, New Rectangle(19, 0, 18, 18), GraphicsUnit.Pixel)
Next x
Next y
Why is is bad to use a picturebox? | | Samus Thursday, June 28, 2007 1:04 PM | Using Graphics.DrawImage() was good advice. However, don't use PictureBox. Just draw on a Panel or the form with the Paint event. You'd paint your tiles one by one using different x and y coordinates. For example:
private void Form1_Paint(object sender, PaintEventArgs e) { // Example of 8 x 8 grid of tiles, using a resource for the tile image Image img = Properties.Resources.SampleTile; for (int x = 0; x < 8; ++x) { for (int y = 0; y < 8; ++y) { e.Graphics.DrawImage(img, x * img.Width, y * img.Height); } } }
| | nobugz Thursday, June 28, 2007 12:08 PM | Use Dim G As Graphics = PBoxPlayarea.CreateGraphics() instead of Dim G As Graphics = Graphics.FromImage(PBoxPlayarea.Image)
| | Bruno_1 Thursday, June 28, 2007 12:10 PM |
I think I got the solution, the new image in my code is only to be called the first time. This works for me:
Code Snippet
Dim x, y As Byte
PboxPlayarea.Image = New Bitmap(180, 180)
Dim G As Graphics = Graphics.FromImage(PboxPlayarea.Image)
For y = 0 To 10
For x = 0 To 10
G.DrawImage(PBoxCopy.Image, x * 18, y * 18, New Rectangle(19, 0, 18, 18), GraphicsUnit.Pixel)
Next x
Next y
Why is is bad to use a picturebox? | | Samus Thursday, June 28, 2007 1:04 PM |
Hmm... using creategraphics only shows the picture for less than a second, then the picturebox is blank?
Anything else I should alter in the code, and why is FromImage bad? | | Samus Thursday, June 28, 2007 1:07 PM |
|