【发布时间】:2017-06-20 06:06:41
【问题描述】:
我正在尝试在运行时更改 pictureBox.Image。我有几个存储图片的模型类,每当我单击 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 加载图片如下:
这将在折叠类中设置一个图像,然后该图像将被操作并存储在 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;
}
【问题讨论】:
-
fold.LastEvaluationImage的类型是什么? -
它是一张图片 (jpeg)
-
.Net 数据类型
Image?你是从文件还是资源中加载它? -
我将它加载到“LastEvaluationImage”中,代码在问题中,最后一部分,它在 1 中被修改。我发布的代码,然后保存在“当前”中,然后存储在最后fold.LastEvaluationImage.( with set this.LastEvaluationImage_intern = value;) 中的代码是您的意思吗?或者你想要别的东西?感谢您的宝贵时间
-
您是否尝试将 jpeg 文件直接设置为
PictureBox的源?仅用于调试目的。可能是文件有问题
标签: c# image picturebox