question

ConCorp-5062 avatar image
0 Votes"
ConCorp-5062 asked Viorel-1 answered

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

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);
                     }
                 }


windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.