Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want save the information of the graphics that i paint. And it can show when the windows show again. Most are Irregular graphics. Please Help me !Thanks!

I want to save the drawn into the DataBase ,so Next time i opening the window that i can see what i drawn and drawn where!
Posted
Updated 23-Apr-11 22:01pm
v3
Comments
Dave Kreskowiak 24-Apr-11 0:59am    
You're going to have to explain the background of your problem a bit more. I doubt anyone has any clue what your talking about.
TweakBird 24-Apr-11 1:08am    
Not clear. Where is the problem.? what is the problem.?
Sandeep Mewara 24-Apr-11 1:51am    
Not clear.

1 solution

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.
 
Share this answer
 
v2
Comments
AngelLoose 5-May-11 5:24am    
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.
OriginalGriff 5-May-11 5:42am    
Answer updated
AngelLoose 5-May-11 20:44pm    
Thanks
AngelLoose 5-May-11 20:48pm    
And I also have a problem about it.When I try to change the position that I drawn use the up method can achieve?If not,Please Give me an example!Thanks!
OriginalGriff 6-May-11 4:13am    
Yes: all you have to do is change the information in your list of rectangles, and Invalidate the Form/Panel/whatever you are drawing on. This causes the system to issue the appropriate Paint events.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900