|
|
|
start date: Sun, 01 Jul 2007 06:16:22 -0700,
posted on: microsoft.public.dotnet.framework.drawing
back
| Thread Index |
|
1
nvx
|
|
2
nvx
|
|
3
Göran Andersson
|
How to pass a Graphics object to another method, modify it, and then get it back
Hello there,
could someone please tell me how should I pass a Graphics object
(which has already been drawn onto) to another method to do some
drawing and then get it back from the method with the changes made
there?
An example to clarify the situation:
// this is done after calculating a graph to be displayed
Bitmap bitmap = new Bitmap(pictureBoxGraph.Width,
pictureBoxGraph.Height);
Graphics graph = Graphics.FromImage(bitmap);
SolidBrush bkg = new SolidBrush(System.Drawing.Color.FromArgb(245,
245, 245));
graph.FillRegion(bkg, new Region(new Rectangle(0, 0, bitmap.Width,
bitmap.Height))); // draw the background
// some initialization drawing is done here
// --------------------------------------------------
// This is the place I need to call some methods to draw the parts of
the final graph.
//
// Since the object "graph" already contains something, I need to pass
it to those
// methods to process it further and then it's necessary to get it
back (unless I want
// to lose all the changes that has been made inside the method, which
I don't).
// --------------------------------------------------
Graphics pbGraphics = pictureBoxGraph.CreateGraphics(); // create
graphics on a PictureBox
pbGraphics.DrawImageUnscaled(bitmap, new Point(0, 0)); // flush the
drawn bitmap onto the PictureBox
graph.Dispose();
// some other code here
I tried to pass it with the "ref" keyword but the "graph" became
empty. This means I have also lost all the changes I have made before
calling any method outside the one in which the above code is.
Thanks for any help...
With regards
nvx
Date:Sun, 01 Jul 2007 06:16:22 -0700
Author:
|
Re: How to pass a Graphics object to another method, modify it, and then get it back
SOLVED... The "bitmap" object was not declared properly...
nvx
nvx napsal(a):
> Hello there,
> could someone please tell me how should I pass a Graphics object
> (which has already been drawn onto) to another method to do some
> drawing and then get it back from the method with the changes made
> there?
>
> An example to clarify the situation:
>
> // this is done after calculating a graph to be displayed
> Bitmap bitmap = new Bitmap(pictureBoxGraph.Width,
> pictureBoxGraph.Height);
> Graphics graph = Graphics.FromImage(bitmap);
> SolidBrush bkg = new SolidBrush(System.Drawing.Color.FromArgb(245,
> 245, 245));
> graph.FillRegion(bkg, new Region(new Rectangle(0, 0, bitmap.Width,
> bitmap.Height))); // draw the background
>
> // some initialization drawing is done here
>
> // --------------------------------------------------
> // This is the place I need to call some methods to draw the parts of
> the final graph.
> //
> // Since the object "graph" already contains something, I need to pass
> it to those
> // methods to process it further and then it's necessary to get it
> back (unless I want
> // to lose all the changes that has been made inside the method, which
> I don't).
> // --------------------------------------------------
>
> Graphics pbGraphics = pictureBoxGraph.CreateGraphics(); // create
> graphics on a PictureBox
> pbGraphics.DrawImageUnscaled(bitmap, new Point(0, 0)); // flush the
> drawn bitmap onto the PictureBox
> graph.Dispose();
>
> // some other code here
>
>
>
> I tried to pass it with the "ref" keyword but the "graph" became
> empty. This means I have also lost all the changes I have made before
> calling any method outside the one in which the above code is.
>
> Thanks for any help...
>
>
> With regards
> nvx
Date:Sun, 01 Jul 2007 06:56:04 -0700
Author:
|
Re: How to pass a Graphics object to another method, modify it, and
then get it back
nvx wrote:
> SOLVED... The "bitmap" object was not declared properly...
>
> nvx
>
I think that you are a bit confused about how it works, so I'll try to
clear some things up... :)
>
> nvx napsal(a):
>> Hello there,
>> could someone please tell me how should I pass a Graphics object
Just pass the reference to the object as usual. You never pass the
object itself to a method, the object remains on the heap all the time.
>> (which has already been drawn onto)
You don't draw on a Graphics object, you draw on a bitmap. The bitmap
can be something like a Bitmap object or the screen.
>> to another method to do some
>> drawing and then get it back from the method with the changes made
>> there?
You don't need to get the object back, as you never pass it to the
method in the first place. You just pass a reference to the object.
Also, as what you draw is drawn on the bitmap, not the Graphics object,
the Graphics object doesn't change.
>>
>> An example to clarify the situation:
>>
>> // this is done after calculating a graph to be displayed
>> Bitmap bitmap = new Bitmap(pictureBoxGraph.Width,
>> pictureBoxGraph.Height);
>> Graphics graph = Graphics.FromImage(bitmap);
>> SolidBrush bkg = new SolidBrush(System.Drawing.Color.FromArgb(245,
>> 245, 245));
>> graph.FillRegion(bkg, new Region(new Rectangle(0, 0, bitmap.Width,
>> bitmap.Height))); // draw the background
What about simply:
graph.Clear(System.Drawing.Color.FromArgb(245, 245, 245));
>>
>> // some initialization drawing is done here
>>
>> // --------------------------------------------------
>> // This is the place I need to call some methods to draw the parts of
>> the final graph.
>> //
>> // Since the object "graph" already contains something, I need to pass
>> it to those
>> // methods to process it further and then it's necessary to get it
>> back (unless I want
>> // to lose all the changes that has been made inside the method, which
>> I don't).
>> // --------------------------------------------------
>>
>> Graphics pbGraphics = pictureBoxGraph.CreateGraphics(); // create
>> graphics on a PictureBox
This will create a Graphics object for the screen, not for the Bitmap.
You will be drawing on the screen where the picture box happens to be,
not on the Bitmap object that the picture box is showing.
>> pbGraphics.DrawImageUnscaled(bitmap, new Point(0, 0)); // flush the
>> drawn bitmap onto the PictureBox
>> graph.Dispose();
>>
>> // some other code here
>>
>>
>>
>> I tried to pass it with the "ref" keyword but the "graph" became
>> empty. This means I have also lost all the changes I have made before
>> calling any method outside the one in which the above code is.
>>
>> Thanks for any help...
>>
>>
>> With regards
>> nvx
>
--
Gran Andersson
_____
http://www.guffa.com
Date:Sat, 14 Jul 2007 02:59:56 +0200
Author:
|
|
|