Friday, November 5, 2010

Bitmaps in VB.Net or What I threw together in a couple minutes

I have been playing a game called Illyriad lately. Now what does this have to do with VB.Net you ask? Not much until you get to the map. In-game you can only see a 13 by  13 chunk of the map at a time so I collected all the data needed and made my own map of the Island my Alliance controls. The map works fine but I wanted to make one big image instead of a lot of tiles put together so I threw something together in Visual Basic 2010.

First I needed to get the separate pictures into the program as bitmaps and make the background color(white) transparent. Because there where a lot I thew them all in an array called Img(). For my example I will have only 2 for simplicity.

        Dim Img(2) as Bitmap
        Img(0) = Image.FromFile("C:\wherever\image\stored.png")
        Img(1) = Image.FromFile("C:\wherever\image\stored2.png")
        Img(0).MakeTransparent(Color.White)
        Img(1).MakeTransparent(Color.White)

Then I put together the map data and stuck that in another array as well. This one called map(,)

        Dim map(,) As Integer = New Integer(2, 2) {{0,1,0,1},{1,0,1,0}}

Now I made some variables that would be needed, including a new bitmap, mapimg, to contain the final image as well as a new Graphics called gra.

        Dim x As Integer
        Dim y As Integer
        Dim mapimg As New Bitmap(200, 100)
        Dim gra As Graphics = Graphics.FromImage(mapimg)

Finally I use some For loops to cycle through all of the map positions, saved the mapimg as map.png, and made it display a message to denote that it was finished seeing as my actual code took a lot longer than the example would.

        For x = 0 To 1
            For y = 0 To 3
                gra.DrawImage(Img(map(x, y)), y * 50, x * 50)
            Next
        Next
        mapimg.Save("C:\whereever\youwant\map.png")
        MessageBox.Show("Done!")

Now you can not tell from the example but the images overlap in my map slightly. The final product of this code ended up as one big picture all together: My Map Complete

1 comment:

  1. Pretty neat! While I still think it would have been easier just to do it in paint, this solution does have the benefit of allowing you to do the same for the ENTIRE world map once the data is released, and that time save is nothing to scoff at.

    ReplyDelete