Update the drawing without deleting the previous one.

I created this as part of the drawing method

private void draws()
{
 Bitmap bmp = new Bitmap(pictureBox17.Width, pictureBox17.Height);
 using (Graphics g = Graphics.FromImage(bmp))
            {
                //define area do pictureBox17 e preenche a branco
                Brush brush = new SolidBrush(Color.White);
                Rectangle area = new Rectangle(0, 0, pictureBox17.Width, pictureBox17.Height);
                g.FillRectangle(brush, area);
                //desenha as linhas do rectangulo
                g.DrawLine(new Pen(Color.Black), esp, esp, esp, yWcorrigidoesp);
// some more lines
}
 pictureBox17.Image = bmp;
}

And he draws exactly what I want. But imagine that after that I want to update the exact same picture by adding a few lines, without having to draw everything again, is this possible? Obviously I'm using a method

draws();

And then I want to add something, any tips?

+1
source share
2 answers

The way to do this is to create a class DrawActionthat contains all the data necessary for what you want to draw: data Point, Penor Brushetc.

Then you will create and manage List<DrawAction> drawActions, and then you have a choice:

  • "live" Paint PictureBox Panel ( any Paint) .

  • .. Bitmap Image.

: , , ? /? .

, . .

, , , ( ) "", .

.. - , , , , : Color Width LineStyle Pen Points .. .. ..

DrawAction, , , . - , Graphics : Drawxx, Fillxxx, Pen , Colors ..

, a List<Point> a float PenWidth a Color..

, Paint, both:

  • ...
  • .. Image PictureBox.

- Line Polylines.

, Enum , ! cheapo, ;-) Rectangle, FilledRectangle, Ellipse, FilledEllipse, Line, Lines, Polygon, FilledPolygon, Text, Curve, Curves, . Image, GraphicsPath, Spline.. Rotation, Scaling, Gradients, Transparency ..

List<DrawAction> actions = new List<DrawAction>();

public class DrawAction
{
    public char type { get; set; }             // this should be an Enum!
    public Color color { get; set; }     
    public float penWidth { get; set; }        // only one of many Pen properties!
    public List<Point> points { get; set; }    // use PointF for more precision

    public DrawAction(char type_, Color color_, float penwidth_)
    {
        type = type_; color = color_; penWidth = penwidth_;
        points = new List<Point>();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Draw(e.Graphics, actions);
}


public void Draw(Graphics G, List<DrawAction> actions)
{
    foreach (DrawAction da in actions)
        if (da.type == 'L' && da.points.Count > 1)
            using (Pen pen = new Pen(da.color, da.penWidth))
                G.DrawLine(pen, da.points[0], da.points[1]);
        else if (da.type == 'P' && da.points.Count > 1)
            using (Pen pen = new Pen(da.color, da.penWidth))
                G.DrawLines(pen, da.points.ToArray());
    // else..


}

private void button1_Click(object sender, EventArgs e)
{
    AddTestActions();
    pictureBox1.Invalidate();
}

private void button2_Click(object sender, EventArgs e)
{
    AddTestActions();
    Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, 
                            pictureBox1.ClientSize.Height);
    using (Graphics G = Graphics.FromImage(bmp))  Draw(G, actions);
    pictureBox1.Image = bmp;
}

void AddTestActions()
{
    actions.Add(new DrawAction('L', Color.Blue, 3.3f));
    actions[0].points.Add(new Point(23, 34));
    actions[0].points.Add(new Point(23, 134));
    actions.Add(new DrawAction('P', Color.Red, 1.3f));
    actions[1].points.Add(new Point(11, 11));
    actions[1].points.Add(new Point(55, 11));
    actions[1].points.Add(new Point(55, 77));
    actions[1].points.Add(new Point(11, 77));
}

:

enter image description here

+2

Bitmap , , , , Bitmap .

-

Bitmap bmp = new Bitmap(pictureBox17.Width, pictureBox17.Height);
    private void draws()
    {
     if (bmp ==null)
     using (Graphics g = Graphics.FromImage(bmp))
                {
                    //define area do pictureBox17 e preenche a branco
                    Brush brush = new SolidBrush(Color.White);
                    Rectangle area = new Rectangle(0, 0, pictureBox17.Width, pictureBox17.Height);
                    g.FillRectangle(brush, area);
                    //desenha as linhas do rectangulo
                    g.DrawLine(new Pen(Color.Black), esp, esp, esp, yWcorrigidoesp);
                 }
   else {
         using (Graphics g = Graphics.FromImage(bmp))
         {
            g.DrawLine(new Pen(Color.Black), esp, esp, esp, yWcorrigidoesp);
         }
    // some more lines
    }
     pictureBox17.Image = bmp;
    }

..:)

bmp draw, bmp

+1

All Articles