Jump to content

Graphics Problem in C#


Selacius

Recommended Posts

I'm trying to use some code I found on a different topic: http://www.gamedev.net/community/forums/to...topic_id=429322 to create a map editing program for my online RPG. I've gotten most of it figured out except for the DrawImage command. This is the code I have at the moment.**MYSQL Information**baseloc = OdbcDR[4].ToString();Bitmap bmp = new Bitmap(System.Convert.ToInt32(OdbcDR[6])*27, System.Convert.ToInt32(OdbcDR[7])*27);mappicbox.Image = bmp;// do every time you redrawGraphics gfx;for (int x=0;x<=System.Convert.ToInt32(OdbcDR[6]); x++) {for (int y=0;y<=System.Convert.ToInt32(OdbcDR[7]);y++){**Mysql Information**topx = x*27;topy = y*27;Image tile = Image.FromFile(@"**File Path**"+baseloc+"\\"+OdbcDR[2].ToString()+".bmp");gfx.DrawImage(tile, topx, topy, 27, 27);}}Basically it is telling me that the variable gfx is a local unassigned variable. And when i use Graphics.DrawImage it tells me that an object reference is required for the non-static field, method, or property 'System.Drawing.Graphics.DrawImage', and if i try to use e.Graphics i get System.EventArgs does not contain a definition for 'Graphics'.What I am trying to do is, create a graphic which is composed of the multiple tiles in a map db. For example the graphic will be 25tiles long by 25tiles in height. Then I want to convert this to a bmp to add as an image for a picturebox.Any help would be greatly appreciated.

Link to comment
Share on other sites

are you getting this error at runtime or on compile?Try giving Graphics gfx; a value even if it is null. The compiler looks at the variable getting set inside the for loops but throws this error because the loops might not run (depending on values,etc) so it really is saying "You are trying to use an object that might not be set yet" so it wants you to initialize it even if it is to null.Hopefully this helps

Link to comment
Share on other sites

I can offer two pieces of advice. The first is that you aren't instantiating that gfx object which is why you are getting that object null reference error when you attempt to execute the code. I think you are missing that part of the example that was posted on the link you provided:

bitmap bmp = new Bitmap(picBox.width, picBox.height);picturebox.Image = bmp;// do every time you redrawGraphics gfx = bmp.getGraphics();
The second thing is that you may have quite a bit of overhead on your for loops. For each iteration of your loops, you're telling the computer to look up some value in a data reader (I assume by the name of the object) and then convert that number into an int. Rather than that, I would suggest changing it to this:
int column6 = Convert.ToInt32(OdbcDR[6]);int column7 = Convert.ToInt32(OdbcDR[7]);for (int x=0;x<=column6; x++){	for (int y=0;y<=column7;y++)	{	}}

For two loops that went from 0 to 4 (5 iterations) nested within one another, there'd be 25 iterations total. The way you had it, the computer would have to look up OdbcDR[7] and convert it to an int 25 times rather than just once.

Link to comment
Share on other sites

For the first bit, thats what the original code used to be. But it kept giving me an error saying System.Bitmap (i think thats wat it was), didn't have a definition or something for GetGraphics(). So I ended up doing this: Graphics gfx = System.Drawing.Graphics.FromImage(bmp);As for the second part, thanks. I will take that into account and make the necessary modifications.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...