Why StreamWriter dosent work good inside a paint event? Getting big red x in the pictureBox
<p>I have a pictureBox1 paint event.
In the paint event im writing to a text file some information:</p>

<pre><code>    private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
                SolidBrush brush;
                Pen p=null;
               Point connectionPointStart;
               Point connectionPointEnd;
               Graphics g = e.Graphics;
               g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                   brush = new SolidBrush(Color.Red);
                   p = new Pen(brush);
                   for (int idx = 0; idx &lt; wireObject1._point_X.Count; ++idx)
                   {
                       Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
                       dPoint.X = dPoint.X - 5; // was - 2
                       dPoint.Y = dPoint.Y - 5; // was - 2
                       Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
                       g.FillEllipse(brush, rect);

                       // g.FillEllipse(brush, rect);
                   }

                   for (int i = 0; i &lt; wireObject1._connectionstart.Count; i++)
                   {

                       int startIndex = wireObject1._connectionstart[i];
                       int endIndex = wireObject1._connectionend[i];



                       connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
                       connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
                       p.Width = 4;
                       g.DrawLine(p, connectionPointStart, connectionPointEnd);
                       moveCounter++;
                       textBox1.Text = moveCounter.ToString();
                       if (moveCounter &gt; 10)
                       {
                           w.WriteLine("Number Of Moves ===&gt;   " + moveCounter);
                       }
                   }

            }

In the bottom i have a w.WriteLine....
In the top of Form1 i did StreamWriter w;
In the constructor i did: w = new StreamWriter(@"d:\test.txt");

In the pictureBox1 mouse up event im doing w.Close();

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {

                if (mouseMove == true)
                {
                    Point NewPoint = e.Location;
                    wireObject1._point_X[(int)selectedIndex] = NewPoint.X;
                    wireObject1._point_Y[(int)selectedIndex] = NewPoint.Y;
                    mouseMove = false;
                    if (moveCounter == 0)
                    {
                    }
                    else
                    {
                        w.Close();
                    }
                    WireObjectCoordinatesCloneFrame();

                }

            }
</code></pre>

<p>So i move the mouse its recording writing to the file the information from the variable moveCounter wich is type of int.</p>

<p>The problem is when i make the mouse up event and its closing the file in the pictureBox1 i see a BIG RED X with white background.
The file it self is ok on the hard disk with all the information as i wanted.</p>

<p>But why the big red x is appear in the pictureBox1 ? If i remove the w.WriteLine from the paint event so i dont see the big red x.</p>

<p>What can i do to solve it ?</p>
