
Signature
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
>> I am trying to get to grips with some basic graphics, being new to C#.
>> I have this code (see below) in a simple project .. when I run it 6 text
[quoted text clipped - 28 lines]
>
> Chris
On Jan 11, 4:56 am, "Bob Powell [MVP]" <b...@spamkillerbobpowell.net>
wrote:
> Funny, I was just about to suggest that myself ;-)
>
[quoted text clipped - 44 lines]
>
> > Chris
Hi Bob,
I love your site! When will your book be out? ;)
I noticed on your site that you have started a WPF faq. Have had a
chance to use WPF much? What are your impressions of it compared to
GDI and GDI+ ?
Cheers,
Chris
Thanks for all this help .. Bob's site looks really good.
One more thing. I have another problem (always another!)
I am using protected override bool ProcessCmdKey to control the movement
of this circle (see below) by adjusting x and y values .. it works fine,
but I also want the circle to leave a trace behind it as it moves .. I
would think that writing to a Bitmap would be the thing, but I can't
seem to figure out how.
Any help again much appreciated
==
Brian
==
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillEllipse(Brushes.Blue, x, y, 20, 20);
g.Dispose()
}
Peter Duniho - 12 Jan 2008 18:33 GMT
> [...]
> but I also want the circle to leave a trace behind it as it moves .. I
> would think that writing to a Bitmap would be the thing, but I can't
> seem to figure out how.
> Any help again much appreciated
Well, the basic idea would be to create a Bitmap instance into which you
draw, using Graphics.FromImage() to get a Graphics instance you can use
for drawing the circle, then in your Paint handler draw the Bitmap instead
of the circle.
Assuming the size of your control never changes, this may be the easiest.
If you want to handle changes in the control size gracefully it gets more
complicated, at least partly just because you then need to decide what
behavior is "correct" for your application.
You could save the circles into a Metafile instead, which introduces its
own complexities but can avoid the resizing issue. Alternatively, you
could just keep your own list of circles as you add them, drawing all of
them one by one each time the control needs to be painted. And of course
you could use some combination of the above, such as using a Bitmap most
of the time, but still storing a list of the circles so that you can
recreate a new Bitmap at arbitrary sizes as the control changes without
worrying about how best to preserve previously drawn circles.
Basically, for small numbers of circles, just storing the circles
themselves is easiest and more efficient. As the number of circles goes
up, a solution that uses only a Bitmap becomes more efficient. There's a
large area of wiggle room in the middle where you can pretty much do it
however you prefer. :)
Pete
Peter Duniho - 14 Jan 2008 04:28 GMT
I was looking at this thread in review for a reply elsewhere, and I took
another look at this post. I'm embarassed that I didn't notice the
obvious bug, especially since it might have been introduced as a result of
a misunderstanding of a comment I made earlier. In particular:
> [...]
> private void Form1_Paint(object sender, PaintEventArgs e)
[quoted text clipped - 3 lines]
> g.Dispose()
> }
Do _not_ dispose the Graphics instance. It is good and correct to dispose
objects that are disposable and which you created in your own code. But
the Graphics instance is passed to you in the event args and must remain
valid for other Paint event handlers. Don't call Dipose() on it.
Sorry for not noticing this earlier.
Pete