How to draw points on a panel with an image

Refresh

December 2018

Views

1.4k time

2

I have a panel named dPanel. I set the the backgroundImage of that panel with an image named dImage.Now I want to draw points on the panel, in other words I want to color the panel by using mouse.I want to be able to save the drawing and the image together later. My codes do this but the picture lights up during drawing and its very slow.Here is my code:

 private void drawP_MouseDown(object sender, MouseEventArgs e)
    {
        if (!drawbool)
        {
            dStartPoint = e.Location;
            drawbool = true;
        }
        drawP.Invalidate();
    }
 private void drawP_MouseMove(object sender, MouseEventArgs e)
    {
        if (drawbool)
        {
            dStartPoint = e.Location;
            drawP.Invalidate();
        }
    }
 private void drawP_MouseUp(object sender, MouseEventArgs e)
    {
        if (drawbool)
        {
            drawbool = false;
        }
    }
  private void drawP_Paint_1(object sender, PaintEventArgs e)
    {
        if (drawbool)
        {
            int dStartX = dStartPoint.X;
            int dStartY = dStartPoint.Y;
            e.Graphics.DrawEllipse(dP, dStartX, dStartY, 2, 2);
            Bitmap dPPB = new Bitmap(drawP.Width, drawP.Height);
            drawP.DrawToBitmap(dPPB, new Rectangle(0, 0, drawP.Width, drawP.Height));
            drawP.BackgroundImage = (Image)dPPB;
        }
    }

Whani's the solution? Thanks in advance.

1 answers

1

Проверьте это отличный пример о рисовании на панели: Живопись на панели .

Позже вы можете сохранить рисунок просто вызвав метод панели в DrawToBitmap .