473,769 Members | 6,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(obj ect 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(UnitFile Name);
}

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

Point keypoint = new Point(200, 200);

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

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

}
Nov 30 '06 #1
2 3105
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.FromHw nd ( 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(o bject sender, EventArgs e)
{
string tempfilename = "d:/Artistic
Applications/Enterprise.jpg" ; // the Sprite bitmap
Sprite Enterprise = new Sprite(tempfile name);
using ( Graphics g = Graphics.FromHw nd ( 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(obj ect 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(UnitFile Name);
}

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

Point keypoint = new Point(200, 200);

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

private void button1_Click(o bject sender, EventArgs e)
{
string tempfilename = "d:/Artistic
Applications/Enterprise.jpg" ; // the Sprite bitmap
Sprite Enterprise = new Sprite(tempfile name);
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
1694
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
1689
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 elegantly: I'm using a templated container that I created for this project that sorts objects based on a numerical value. That is, each object in the container has an integer value associated with it, and objects with lower integer values come...
8
1465
by: twoeyedhuman1111 | last post by:
Okay, I have this sample code: class a; class b; class b { public: private:
3
2577
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 200x200, but it's drawing something smaller (something like 130x130, about 2/3 the original size). I am using transforms, but I've verified that scaling is set to 1.0 on both x and y axis.
5
1060
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, and I want to know what is the easiest way to add labels or textboxes to a form in runtime. Any help hint and tips would be welcome
14
1416
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
1695
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 visual c# express edition 2005 as well as sql 2005 express. Here's the thing I've been trying to figure out. I want to insert a new row in the Employees Table when I click a button called btnAdd. When I was learning delphi, I would just put the...
0
1410
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, games.Mover): """ A falling pizza. """ def __init__(self, screen, x, y, image, dx, dy):
18
8585
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 32x32 right next to each other. I assume that the program gets each of thoe tiles and displays them in sequence, I just want to know how to do it myself. I have a sprite that I'd like to use as a loading animation while I'm connecting to a database. ...
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.