Jump to content
  • Advertisement
Sign in to follow this  
Selacius

Unity [.net] Graphics Problem in C#

This topic is 4229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to use some code I found on a different topic: http://www.gamedev.net/community/forums/topic.asp?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 redraw Graphics 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(@"C:\\xampplite\\htdocs\\Arakaron\\game\\Images\\map\\"+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'. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Well what do you want to draw to? If it's a control(like a form or panel) then assign gfx using Drawing.Graphics.FromHwnd, if it's a Drawing.Bitmap use Drawing.Graphics.FromImage

Share this post


Link to post
Share on other sites
Well i figured the easiest way to do this, and what i understood from the other topic is, to create a graphic composed of the individual tiles for each (x,y) location. Then convert this to a bitmap and use set it to the image of a picturebox. I'm not sure if this is the easiest and most accurate way to do it, but thats how I am attempting to proceed.

Share this post


Link to post
Share on other sites
A much better way would be to overload the form's OnPaint method and do your painting there using the passed in Graphics object.

Also, some things you may want to set on the form:
DoubleBuffer = true;
ResizeRedraw = true;
Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyle.UserPaint, true);

If you need more help feel free to ask.

Share this post


Link to post
Share on other sites
I'm not too sure what to do, because I can't even create the gfx graphics. I've tried everything I can, but it keeps giving me the error I presented in my first post.

Share this post


Link to post
Share on other sites
Your problem is that Graphics gfx; just produces a null reference. It hasn't been assigned to refer to any actual Graphics instance yet. Try this with an additional bitmap:
Graphics gfx = System.Drawing.Graphics.FromImage(bmp);
This will make a graphics object that uses the specified bitmap as its underlying image source. Then, all .DrawImage() calls you make to it will be drawn to the bitmap. Later, you can do whatever you want with this bitmap, including drawing it to other Graphics objects, such as the one given to you in the OnPaint method.

Or you could do what others have suggested and use an already existing Graphics object. Depending on what mappicbox is, it may contain a .Graphics property. Then you can just do:
Graphics gfx = mappicbox.Graphics;

Share this post


Link to post
Share on other sites
Well I've got it working, except for two small problems. My maps are looking all screwy, and I need to figure out a how that I can check to see if a tile is located in the file path. If it isn't then replace it with a blank image. Also, I would like to try to find a way that I can check to see if the database has a value in it. To make the tables smaller I removed some of the base repetetive tiles such as grass, dirt, etc. Each map has its own base tile (dungeon has dirt, etc). So if there isn't a value in the table for say (3,3) then I want to make the tile the default tile. Anyone know of a way to check for this? I can do it in php, but unsure how to do it in C#.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!