954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying Graphics on a Form

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.

ejohns85
Newbie Poster
5 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Perhaps my code snippet of plotting on a form might help http://www.daniweb.com/code/snippet1020.html

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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.

hieuuk
Light Poster
44 posts since Nov 2008
Reputation Points: 11
Solved Threads: 4
 

My code draws bitmaps as "points" in a Graphics object it gets from the Forms Paint event.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

I did a game of life in c# the drawing speed wasnt overly noticable..

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You