简体   繁体   中英

C# picture box Image dissapearing

I'm using a picture box in my C# code with a given image. i do all the paint actions in the Pain event (see code below). when the image needs to be updated (ading dots on it) i call Invalidate to make it repaint. however after some time the image and the dots dissapear and are replaced by e big red X in a red border filling up the picture box. note that the image is saved in the save folder as the exe file. and is appearing ok at first (same with the dots).

what am i doing wrong? Crossthread problem?

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    setImage = Image.FromFile("IMG_1612.png");
    Pen p = new Pen(Color.Red);
    var g = Graphics.FromImage(setImage);//e.Graphics;

    g.DrawImage(setImage,0,0);

    foreach (Circles element in _circles)
    {
        g.FillEllipse(new SolidBrush(element.color), element.Punt.X, element.Punt.Y, _CIRCLESIZE, _CIRCLESIZE);
    }

    this.pictureBox1.Image = setImage;
    g.Dispose();

}

delegate void PicturBoxUpdate(Control control);

private void UpdatePictureBox(Control control)
{
    // InvokeRequired required compares the thread ID of the  
    // calling thread to the thread ID of the creating thread.  
    // If these threads are different, it returns true.  
    if (control.InvokeRequired)
    {
        PicturBoxUpdate d = new PicturBoxUpdate(UpdatePictureBox);
        this.Invoke(d, new object[] { control});
    }
    else
    {
        control.Invalidate();
        control.Update();
    }
}

private void DataProcessing(string data)
{            
    data = data.Replace('<', ' ').Replace('>',' ').Trim();
    string[] processingdata = data.Split(';');
    Circles tempCircle;
    for (int i = 0; i < processingdata.Length-2;i++)
    {
        _data[i] = Convert.ToByte(processingdata[i], 16);
        BitArray localdata = new BitArray(BitConverter.GetBytes(_data[i]).ToArray());
        switch(i)
        {
            case 0:
                if (_data[i] == 0xAA)
                {
                    tempCircle = _circles[5];
                    tempCircle.color = Color.Green;
                    _circles[5] = tempCircle;

                    tempCircle = _circles[4];
                    tempCircle.color = Color.Green;
                    _circles[4] = tempCircle;
                }
                else
                {
                    if (localdata.Get(1) & !localdata.Get(2))
                    {
                        tempCircle = _circles[5];
                        tempCircle.color = Color.Orange;
                        _circles[5] = tempCircle;

                        tempCircle = _circles[4];
                        tempCircle.color = Color.Orange;
                        _circles[4] = tempCircle;
                    }
                    if (localdata.Get(3) & !localdata.Get(0))
                    {
                        tempCircle = _circles[5];
                        tempCircle.color = Color.Blue;
                        _circles[5] = tempCircle;
                    }
                    if (localdata.Get(5) & !localdata.Get(4))
                    {
                        tempCircle = _circles[4];
                        tempCircle.color = Color.Blue;
                        _circles[4] = tempCircle;
                    }
                    if (localdata.Get(7) & !localdata.Get(6))
                    {
                        tempCircle = _circles[4];
                        tempCircle.color = Color.Purple;
                        _circles[4] = tempCircle;

                        tempCircle = _circles[5];
                        tempCircle.color = Color.Purple;
                        _circles[5] = tempCircle;
                    }
                }

                break;
            case 1:                            
                break;   
            default:
                break;
        }    
    }

    UpdatePictureBox(pictureBox1);
    GC.Collect();

}

You definitely shouldn't be using Image.FromFile() in the Paint() event like that; set it once and simple draw your circles using the supplied Graphics in the Paint() event:

private Image setImage;

private void Form1_Load(object sender, EventArgs e)
{
    setImage = Image.FromFile("IMG_1612.png");
    pictureBox1.Image = setImage;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    foreach (Circles element in _circles)
    {
        using (SolidBrush B = new SolidBrush(element.Color))
        {
            g.FillEllipse(B, element.Punt.X, element.Punt.Y, _CIRCLESIZE, _CIRCLESIZE);
        }                
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM