Well, you could start by looking at the way you tell the Paint method what it has to draw.
Since you have to tell it something, that something is what you need to save.
How to save it? Can't say - without knowing at least something about what you are drawing from, and what media you have available to store into (database, text file, config file, your own datafile format, etc.) we really can't be a lot more helpful. Tell us more about what you are doing and need to do, and we will give you better information!
"Such as: Graphics g=new Graphics (); Brush b=new SolidBrush(Color.Blue); Pen p=new Pen(b); g.DrawRectangle(p,New Rectangle(10,20,30,40)); ---------------------------------------------------- Up the Code I write ,I Paint a Rectangle.And I want to save the Rectangle's Properties,When next time I run the Application then show the Rectangle again."
Firstly, don't do that! Why not?
1) If you create a Graphics object you are responsible for disposing of it - they are a scarce resource, and you will run out of them a lot faster than the Garbage Collector get get round to Disposing them! This will cause unpredictable errors in you program. This also applies to Brushes and Pens - so move them outside your painting method if they never change!
2) If you are handling the Paint event then you are handed the appropriate Graphics object as part of the PaintEventArgs parameter.
Graphics g = e.Graphics;
If you aren't using the Paint event, you should be, or your drawings will not be persistent in the form - if you minimise it and then restore, your rectangle will disappear.
Why not create a List<Rectangle> which contains the rectangles you want to draw?
List<rectangle> myListOfRectangles = new List<rectangle>();</rectangle></rectangle>
Then in your Paint event:
private void myPanelToDrawOn_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (Brush b = new SolidBrush(Color.Blue))
{
using (Pen p = new Pen(b))
{
foreach (Rectangle r in myListOfRectangles)
{
g.DrawRectangle(p, r);
}
}
}
}
If you create the list at class level, you can use
myListOfRectangles.Add(new Rectangle(x, y, w, h));
when you need to, and follow it with a call to the Invalidate method to redraw the lot.
Then all you have to do is save and restore the Rectangles to a file when you stop and start your app.