How to Save Images with draw pint and labels on it? in #c

ConCorp 1 Reputation point
2021-06-04T05:59:48.813+00:00

when I try to save my image all paint and labels disappear here is my code :

 //paint on picture box
        bool startPaint = false;
        Graphics g;
        int? initX = null;
        int? initY = null;
        bool drawSquare = false;
        bool drawRectangle = false;
        bool drawCircle = false;


        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {

            startPaint = false;
            initX = null;
            initY = null;
        }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
                {



            startPaint = true;
                    if (drawSquare)
                    {
                        //Use Solid Brush for filling the graphic shapes
                        SolidBrush sb = new SolidBrush(toolStripButton6.BackColor);
                        //setting the width and height same for creating square.
                        //Getting the width and Heigt value from Textbox(txt_ShapeSize)
                        g.FillRectangle(sb, e.X, e.Y, int.Parse(cmb_PenSize.Text), int.Parse(cmb_PenSize.Text));
                        //setting startPaint and drawSquare value to false for creating one graphic on one click.
                        startPaint = false;
                        drawSquare = false;
                    }
                    if (drawRectangle)
                    {
                        SolidBrush sb = new SolidBrush(toolStripButton6.BackColor);
                        //setting the width twice of the height
                        g.FillRectangle(sb, e.X, e.Y, 2 * int.Parse(cmb_PenSize.Text), int.Parse(cmb_PenSize.Text));
                        startPaint = false;
                        drawRectangle = false;
                    }
                    if (drawCircle)
                    {
                        SolidBrush sb = new SolidBrush(toolStripButton6.BackColor);
                        g.FillEllipse(sb, e.X, e.Y, int.Parse(cmb_PenSize.Text), int.Parse(cmb_PenSize.Text));
                        startPaint = false;
                        drawCircle = false;
                    }

                    //Text

                }

 private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

                    if (startPaint)
                    {
                        //Setting the Pen BackColor and line Width
                        Pen p = new Pen(toolStripButton6.BackColor, float.Parse(cmb_PenSize.Text));
                        //Drawing the line.
                        g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new 
                        Point(e.X, e.Y));
                        initX = e.X;
                        initY = e.Y;
                    }
                }

 private void toolStripButton7_Click(object sender, EventArgs e)
                {
                    ColorDialog c = new ColorDialog();
                    if (c.ShowDialog() == DialogResult.OK)
                    {
                        pictureBox1.BackColor = c.Color;
                        toolStripButton7.BackColor = c.Color;
                    }
                }

                private void toolStripButton8_Click(object sender, EventArgs e)
                {
                    drawSquare = true;
                }

                private void toolStripButton9_Click(object sender, EventArgs e)
                {
                    drawRectangle = true;
                }

                private void toolStripButton10_Click(object sender, EventArgs e)
                {
                    drawCircle = true;
                }

                private void toolStripButton11_Click(object sender, EventArgs e)
                {
                    FontDialog dlg = new FontDialog();
                    dlg.ShowDialog();

                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        string fontName;
                        float fontSize;
                        fontName = dlg.Font.Name;
                        fontSize = dlg.Font.Size;
                        MessageBox.Show(fontName + "    " + fontSize);
                    }
                }
Developer technologies | Windows Forms
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-06-04T09:07:05.113+00:00

    If you assign some initial image to picturebox:

    pictureBox1.Image = new Bitmap( 300, 200 );
    

    and draw using an approach like this:

    using( var g = Graphics.FromImage( pictureBox1.Image ) )
    {
       // example:
        g.FillRectangle( Brushes.Red, new Rectangle( 10, 10, 100, 50 ) );
       . . .
    }
    pictureBox1.Refresh( );
    

    then the drawn image will be kept. You can save it using pictureBox1.Image.Save(…) function.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.