Hello,

I'm a bit of a newbie here, so would appreciate some help on this one!

I want to write a program that will display a map on a windows form. The map is built up of a 2-dimensional array of objects (either Land or Water), of size [1000, 1000], which represent whether that area of the map is land, or water. Eg. at Map[30, 50] there is a Land object, which means that at coordinates (30, 50), there is one square of land on the map. The Land and Water classes have a member called "colour", which defines the colour of that square on the map. Each square on the map has a side of length 1. So the map is just a load of squares all joined together.

Ok, so I now want to display this map on a form. To do this, I created a pictureBox on the form, and then created a graphics object, g, from this pictureBox. Next, I performed the following operations:

for (int i = 0; i < 1000; i ++)
{
for (int j = 0; j < 1000; j ++)
{
g.FillRectangle(new Pen(Map[i, j].colour).Brush, new Rectangle(i, j, 1, 1));
}
}

This basically colours in each square at coordinates (i, j) with the colour corresponding to whether it is land, or water, (green, or blue), at that location.

Essentially, this works. However, it is soooo slow to draw, and takes about 3 seconds just to draw the map. The map sweeps across the pictureBox over a period of 3 seconds, rather than just instantly displaying the map. This is not acceptable for the purpose of the program.

Therefore, I am wondering what the best way would be to display this map. I have 2 ideas, but I wouldn't know how to implement them. They are :

* Using graphics buffers, which I have heard about, but have no idea how they work. Can anybody tell me how to use them?
* Using DirectX rather than the basic graphics I am using (is it called GDI?)

Please could anybody advise on how to improve the performance of this program, so that I can just display the map instantly.

Thank you for your help :)

Ed.

Haven't got chance to read Ddanbe code snippet carefully yet but I think it would be alright if you still using GDI though. What you should do is instead of drawing straight onto the control, you should draw onto a bitmap variables. After finish drawing the bitmap, set the bitmap as picturebox.image. This using the same theory as Game Programming, finish draw first before show the result.

I made a Graphic Editor before and it work fine using GDI so I think yours should be the same.

This article has been dead for over six months. Start a new discussion instead.