C# - 在运行时更新pictureBox.Image导致错误

时间:2017-06-20 06:06:41

标签: c# image picturebox

我想在运行时更改pictureBox.Image。我有几个存储图片的Model类,每当我点击MenuStripItem时我都会调用方法“ChangePictureBoxImages”。直到那时没有错误(pB是不可见的!)但是一旦我调用方法使pB可见,我得到一个错误。错误代码:“System.Drawing.dll中发生了'System.ArgumentException'类型的未处理异常”。

研究表示我应该处理图片框并将其设置为“null”,但这没有帮助。

我的代码:

using (Image current = BitmapManipulator.EvaluateMesurement(CSV_Name1, max_Rows, max_Col, var.TopImage, var.BitmapToManipulate, pB_ColourScale_Evaluation.Image, var.BitmapToManipulate, var.Filepath, var.FoldID))
                {

                    var.LastEvaluationImage = current;
                    BitmapManipulator.CombineImagesAndSaveThem_Evaluation(var.TopImage, var.BitmapToManipulate, pB_ColourScale_Evaluation.Image, var.Filepath, var.FoldID);  //saves the Files as jpg 
                    if (var.CurrentlyShownToUser) //checks if the MenuStripItem is the active one
                    {
                        if (var.LastEvaluationImage == null) { MessageBox.Show("the image is null");} //only for debugging purpose -> does never call
                        ChangePictureBoxImages(); 

                    }
                }

和ChangePictureBoxImages():

      public void ChangePictureBoxImages()
    {

        foreach (Fold fold in FoldExisting)    
        {
            if (fold.FoldID == LastSelectedMenuStripItem_Name)       //the clicked item is the last Selected MenuStripItem
            {
                if (fold.LastEvaluationImage != null)
                {
                    Debug.WriteLine(pB_Evaluation_Bottom.Image.ToString() + "   " + fold.LastEvaluationImage.ToString());
                    pB_Evaluation_Bottom.Image = fold.LastEvaluationImage;
                }


                pB_Evaluation_Top.Image = fold.TopImage;
            }

        }
    }

在此之前没有错误,一旦我调用“pB_Evaluation_Bottom.visible = true”,就会出现错误。 (或者如果我首先调用可见方法,则首先在更改图像时出现错误!)在MenuStripItem上单击2次时也会出现错误。我从Class Fold加载图片如下:

这将在fold类中设置一个图像,然后该图像将被操作并存储在LastEvaluationImage中

        private void setTheImages(string PictureToManipulate, string PathToTopImage)
    {
        try
        {
            this.BitmapToManipulate_intern = (Image)Image.FromFile(@PictureToManipulate, true);
            this.TopImage_intern = (Image)Image.FromFile(@PathToTopImage, true);
        }
        catch (ArgumentNullException ex)
        {
            Debug.WriteLine("The BitMap for the manipulation process and the top image is not created.");
        }

    }

和存储最后一张图片的LastEvaluationImage - >这将被称为新的pb.Image

        private Image LastEvaluationImage_intern;
    public Image LastEvaluationImage
    {
        get
        {
            return this.LastEvaluationImage_intern;
        }
        set
        {
            if (LastEvaluationImage_intern != null) { LastEvaluationImage_intern.Dispose(); LastEvaluationImage_intern = null; }
            this.LastEvaluationImage_intern = value;
            this.LastEvaluationTime_intern = DateTime.Now;
        }
    }

我知道这有点复杂,但我希望有人可以帮助我。

感谢提前!

更新:错误必须在以下代码中: BitmapManipulator.EvaluateMeasurement代码:

 public Image EvaluateMesurement(double[][] MeasuredValues, int max_Rows, int max_Col, Image pB_Evaluation_Top, Image pB_Evaluation_Bottom, Image pB_EvaluationColourScale, Image ManipulatedBitmap, string PathMeasurementFiles, string Foldname) 
    {

        using (Bitmap bitmap = new Bitmap(ManipulatedBitmap))  
        {
            // the data array sizes:
            int number_nio = 0;
            int number_total = 0;
            List<FileInfo> LastFiles;   
            int got_number_for_trends = Properties.Settings.Default.TrendNumber;



            SolidBrush myBrush = new SolidBrush(red);

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                Random rnd = new Random(8);
                int[,] data = new int[max_Col, max_Rows];



                // scale the tile size:
                float sx = 1f * bitmap.Width / data.GetLength(0);
                float sy = 1f * bitmap.Height / data.GetLength(1);

                LastFiles = FM.GetLastFiles_Trend(ref got_number_for_trends, PathMeasurementFiles);  
                double[][] CSV_Statistiken = FM.LastFilesToCSV(got_number_for_trends, true, LastFiles, PathMeasurementFiles);    


                for (int x = 0; x < max_Col; x++)     
                {
                    for (int y = max_Rows - 1; y >= 0; y--)  
                    {
                        number_total++;
                        RectangleF r = new RectangleF(x * sx, y * sy, sx, sy);

                        if (MeasuredValues[y][x] < Properties.Settings.Default.Threshhold) 
                        {
                            number_nio++;
                            if (CSV_Statistiken[y][x] == Properties.Settings.Default.TrendNumber)  
                            {
                                myBrush.Color = Color.FromArgb(150, black);
                                g.FillRectangle(myBrush, r);
                            }
                            else
                            {
                                myBrush.Color = Color.FromArgb(150, red);
                                g.FillRectangle(myBrush, r);
                            }

                        }
                        else    
                        {

                            myBrush.Color = Color.FromArgb(150, green);
                            g.FillRectangle(myBrush, r);
                        }

                    }


                }
            }

            return bitmap;  
        }
    }

此返回的位图将存储在fold.LastEvaluationImage中,如下所示:

using (Image current = BitmapManipulator.EvaluateMesurement(CSV_Name1, max_Rows, max_Col, var.TopImage, var.BitmapToManipulate, pB_ColourScale_Evaluation.Image, var.BitmapToManipulate, var.Filepath, var.FoldID))
            {

                var.LastEvaluationImage = current;
            }

1 个答案:

答案 0 :(得分:1)

您正在返回已处置的位图。你不能画出不再存在的东西,这不足为奇了。)

在这种情况下,using (bitmap)是你想要的最后一件事。 bitmap 必须的存活时间超过使用范围。调用者中的using (current)也有同样的问题 - 您过早地再次处理图像。您只有在明确表示不会再次使用时才能将其丢弃 - 例如当你用新图像替换它时。

详细说明一下,using除了在离开其范围时只会调用Dispose。在Bitmap(它只是一个&#34;瘦&#34;围绕GDI位图的包装器)的情况下,这将释放存储实际图像数据的存储器。没有任何有趣的东西,所以没有什么可以绘制的(就GDI而言,你基本上都在调用DrawBitmap(NULL)。)