473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Newb question: drawing a sprite

I'm new to C#, and I have only limited programming experience. I've
been doing the video tutorials at MS's website, and they're very
helpful, but I decided to experiment with GDI+ and have gotten stuck.
I'm trying to draw a bitmap on my main form and then to draw a second
bitmap, as if it were a sprite (e.g., a "unit" in a wargame), on top of
the first. The main form renders fine; I handle the Paint event by
creating a temporary Graphics object, and at the same time I grab that
object so that I can use it again to draw the sprite later.

But when I try to draw the sprite, the program compiles but crashes at
runtime. It says my Point parameter is invalid. The sprite itself and
the Pointer seem to contain valid data when this happens. This happens
even if I comment out the DrawImage call that paints the main form.
What am I doing wrong?

Here's a snippet of code. Be gentle; I'm new at this. :) This code is
in my one and only Form, Form1:

public void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
SpriteGraphics = g;
Image img = Image.FromFile("d:/Artistic
Applications/Blue.jpg"); // background for the form
g.DrawImage(img, 150, 150); // this works fine
}

public Graphics SpriteGraphics; /* to grab and reuse the
Graphics object for drawing the sprite; is this the right technique? */

class Sprite
{
protected Bitmap _unit;

public Sprite(string UnitFileName)
{
_unit = new Bitmap(UnitFileName);
}

public Bitmap Unit
{
get { return _unit; }
set { _unit = value; }
}

Point keypoint = new Point(200, 200);

public void Draw(Graphics graphics)
{
graphics.DrawImage(Unit, keypoint ); /* crashes here.
Unit and keypoint both seem to contain the right values, but crashola.
*/
}
}

private void button1_Click(object sender, EventArgs e)
{
string tempfilename = "d:/Artistic
Applications/Enterprise.jpg"; // the Sprite bitmap
Sprite Enterprise = new Sprite(tempfilename);
Enterprise.Draw(SpriteGraphics);

}
Nov 30 '06 #1
2 3074
Hi Carl,

The main problem here is that you try to cache the Graphics object that
is given to your Paint event. That object would be only valid during
that event, so you should not save it.

Generally speaking you should not save the Graphics object at all. You
should create that when you are drawing, and then call Dispose() on it
or use the one provided with the Paint event.

If you want to cache something, like the background where you will be
drawing, you should save a Bitmap, that contains that background. When
you then need it, you can draw onto that bitmap.

Another problem would be, that even though you draw during your Click
event, the next time Paint is called (== the next time your window or
portion of it requires redrawing) your sprite will be erased.

Anyway, to quickly make your code working ...

Do not cache the Graphics object in the Form1_Paint event handler. Then,
in button1_Click you can get a Graphics objects as follows:

Graphics g = Graphics.FromHwnd ( this.Handle );

Make sure to call Dispose() on it, when you are done with it. Or you
could put that into a using statement. For example:

private void button1_Click(object sender, EventArgs e)
{
string tempfilename = "d:/Artistic
Applications/Enterprise.jpg"; // the Sprite bitmap
Sprite Enterprise = new Sprite(tempfilename);
using ( Graphics g = Graphics.FromHwnd ( this.Handle ) )
{
Enterprise.Draw(g);
}
}

Hope this helps,

-Lenard
Carl wrote:
I'm new to C#, and I have only limited programming experience. I've
been doing the video tutorials at MS's website, and they're very
helpful, but I decided to experiment with GDI+ and have gotten stuck.
I'm trying to draw a bitmap on my main form and then to draw a second
bitmap, as if it were a sprite (e.g., a "unit" in a wargame), on top of
the first. The main form renders fine; I handle the Paint event by
creating a temporary Graphics object, and at the same time I grab that
object so that I can use it again to draw the sprite later.

But when I try to draw the sprite, the program compiles but crashes at
runtime. It says my Point parameter is invalid. The sprite itself and
the Pointer seem to contain valid data when this happens. This happens
even if I comment out the DrawImage call that paints the main form.
What am I doing wrong?

Here's a snippet of code. Be gentle; I'm new at this. :) This code is
in my one and only Form, Form1:

public void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
SpriteGraphics = g;
Image img = Image.FromFile("d:/Artistic
Applications/Blue.jpg"); // background for the form
g.DrawImage(img, 150, 150); // this works fine
}

public Graphics SpriteGraphics; /* to grab and reuse the
Graphics object for drawing the sprite; is this the right technique? */

class Sprite
{
protected Bitmap _unit;

public Sprite(string UnitFileName)
{
_unit = new Bitmap(UnitFileName);
}

public Bitmap Unit
{
get { return _unit; }
set { _unit = value; }
}

Point keypoint = new Point(200, 200);

public void Draw(Graphics graphics)
{
graphics.DrawImage(Unit, keypoint ); /* crashes here.
Unit and keypoint both seem to contain the right values, but crashola.
*/
}
}

private void button1_Click(object sender, EventArgs e)
{
string tempfilename = "d:/Artistic
Applications/Enterprise.jpg"; // the Sprite bitmap
Sprite Enterprise = new Sprite(tempfilename);
Enterprise.Draw(SpriteGraphics);

}
Nov 30 '06 #2
Many thanks for your quick reply. I'll give that a try and, knowing me,
I'll be back here with more questions. :P Thanks again.
Nov 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Russell Silva | last post by:
I have a program set up something like this: class A { //(...) } class B : public A { //(...) public: void not_in_class_A();
1
by: Tim Conkling | last post by:
I've run into a problem with a game I'm designing -- I (think) I need to do something ugly, for speed reasons, and I'm wondering if people here might have any ideas about how solve my problem more...
8
by: twoeyedhuman1111 | last post by:
Okay, I have this sample code: class a; class b; class b { public: private:
3
by: Peter Oliphant | last post by:
I'm importing a jpeg via: Bitmap* image = new Bitmap( filename ) ; Then, using the Drawing::Graphics object, I execute DrawImage( image, x, y ). My problem is that the original image was...
5
by: whaletyr | last post by:
I need to add several (a variable number of ) labels in runtime to my form. I am using VB2003 and i have searched for answers and i should change the Index. I cannot find where or how to do it,...
14
by: JoeC | last post by:
vector<Sprite*>box; ++++++++++++++++++++++++++++++++++ tank = new Bitmap(hdc, IDB_IMAGE2, g_hin); Sprite * ms = new Sprite(tank, rcBounds, BA_BOUNCE); break; box.push_back(ms); ...
2
by: uluvale96799 | last post by:
Hi, I'm very new to programming so forgive me for asking the dumb question. I'm trying to develop a database application using c#, and I'm using the northwind database. I'm currently using...
0
by: wwweee | last post by:
I am reading a book about Python Programming. When I practised the following example: from livewires import games SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 class Pizza(games.Sprite,...
18
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I like to go through the Windows files in a resource editor, just to see what's there. I notice a lot of sprite images that are actually animations. For example, a file 32x256 has 8 tiles each...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.