system.drawing namespace question "Graphics"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I was wondering if anyone knows if it is possible in C# given x,y
coordinates of several straight lines to draw these on the screen and then
save this as an image file, also adding text to the file like a title?
 
Yes, it's possible.

You might however be better of drawing it off-screen in a Bitmap object
instead. There is no risk of interference from windows updating
themselves in the area where you are drawing, you don't need to do a
screen capture to get what's drawn, and the size is not limited to the
screen resolution.

Create a Bitmap object, use the Graphics.FromImage method to get a
graphics object, draw on the bitmap using the graphics object, and save
the bitmap as a file.
 
yes, this is easy.
Google is your friend.
http://www.c-sharpcorner.com/2/gdi_plus.asp

look at Graphics.DrawLine()
Graphics.DrawString() and don't forget about Graphics.MeasureString(), it's
very useful

There are tons of examples.

If you want to save as a file, get a Graphics object from a Bitmap of the
size you want
IE:
Graphics g = Graphics.FromBitmap(Bitmap bm);

draw on it
save it
 
ok thanks for the information. I am not too familiar with the graphics
namespace but creating a graphics object does seem to be the way to go. Do
you know of any examples on how to actually write to the object off screen as
you mention?
thanks again
 
thanks for the additional info.
--
Paul G
Software engineer.


Steve said:
yes, this is easy.
Google is your friend.
http://www.c-sharpcorner.com/2/gdi_plus.asp

look at Graphics.DrawLine()
Graphics.DrawString() and don't forget about Graphics.MeasureString(), it's
very useful

There are tons of examples.

If you want to save as a file, get a Graphics object from a Bitmap of the
size you want
IE:
Graphics g = Graphics.FromBitmap(Bitmap bm);

draw on it
save it
 
Paul said:
thanks for the additional info.

While that article discribes the basics, it does not address disposing
of the various objects he creates such as the Pen and Brush objects.

I you create a Pen or Brush, you must call it's Dispose method when you
are done with it, otherwise your program will leak these objects.
 
ok thanks for the response. I have a class that uses the pen, for example to
draw a line,
objGraphics.DrawLine(new Pen (pen), x1,y1,x2,y2) just wondering if in the
destructor of the class would be the appropriate place for the dispose
methods?
 
If you use that code, you can't call the Dispose method at all. You have
to keep a reference to the Pen object that you create.

The destructor is not a good place to call the Dispose method. You have
no control over when the destructor is called. For some objects the
destructor won't be called until the application ends.
 
does the destructor run when the class goes out of scope or is it when the
garbage collector performs a cleanup? thanks for the information.
 
Neither. ;)

The finalizer (not destructor) is run when the garbage collector tries
to collect the object.

(The finalizer should not be confused with a destructor, that is used by
systems that use reference counters instead of a garbage collector, like
C++.)

The finalizer is run instead of collecting the object, so the object
will be collected at the next garbage collection. That means that
objects without a finalizer are collected earlier than objects that has
a finalizer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top