|
-
January 12th, 2012, 10:34 AM
#1
DrawCurve on PictureBox - Disappears when resizing
I have a project I'm working on. I'm drawing a few curve on a PictureBoxs surface and whenever I resize or move the window the Curves disappear. I tried adding the points to List<Point> and on Form_Paint/Form_Resize (or anything else for that matter) it just "flashes by" a bit.
It only works when I click the button that says "Draw" and not in Form_Resize/Paint.
And I can't get the Graphics from my PictureBox because it's not there. The data is elsewhere, however the hell that can be. I can't save the data I've drawn to an Imagefile (JPG, PNG, whatever).
Here is my code:
Code:
private void buttonShowDiagram_Click(object sender, EventArgs e)
{
/* Here I calculate where the points are suppose to be in the Curves according to some data, which works very well... Not the rendering though. */
pictureCurve.CreateGraphics().Clear(Color.White);
/* DrawPoints is a List<> of List<> of Points to draw. DrawPens is a List of Pens. */
for (int i = 0; i < DrawPoints.Count; i++)
{
pictureCurve.CreateGraphics().DrawCurve(DrawPens[i], DrawPoints[i].ToArray());
}
}
If I then reuse this in "Form_Resize/Paint" it just flashes by, although adding a MessageBox there I know it executes everytime I Resize or something gets re-Painted.
Anyone know how I can either (or both) save the Graphics to a simple "IMAGE" so that after I've drawn it can Load the Image and show it? Or get it to "stay visible" all the time?
-
January 13th, 2012, 01:10 PM
#2
Re: DrawCurve on PictureBox - Disappears when resizing
The problem is that you are working outside of the windows message model. Further reading:
http://en.wikipedia.org/wiki/Message...rosoft_Windows
Short explanation; you are doing your drawing in the wrong place. You should be doing all of your drawing inside of a Control's Paint event (or by overriding OnPaint()).
What is happening is that your drawing code runs, you see the update, and then a WM_PAINT message comes through. When this occurs the Control's OnPaint method is called (the method that calls the Paint event) and, since you are not drawing in that method, your drawing is overwritten by the new update.
You almost never need to call CreateGraphics. Also, what you are doing could potentially cause a memory problem as you are not Dispose()'ing of your Graphics object that is created when you call CreateGraphics. So:
1. Move your drawing code into the Paint event for your control and use the supplied Graphics object to draw to (e.Graphics).
2. Read up a bit on the windows message model and how to draw objects in C#.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|