我正在尝试使用线程来处理彼此平行的多个图像。在代码中,您将看到我给了mehtod'imgprocessing'一个位图,但是图像处理仅接收对象参数。因此我必须将其类型转换为位图,并在某些时候抛出异常:“参数无效”。显然,位图在某些时候变为null。 它仅在几个线程已经完成处理后才发生。 问题是:为什么在类型转换期间我的位图为null?我不知道为什么它会停止正常投放。 码- 主要:
int i = 0;
float[] percentcount = new float[6];
string[] sc = Directory.GetFiles(@"C:\Users\User\Desktop\gn clouds-snow", "*.jpg");
Bitmap[] bt = new Bitmap[sc.Length];
Thread[] threads = new Thread[sc.Length];
foreach (string file in sc)
{
Console.WriteLine("number of image: " + i + " / " + sc.Length);
bt[i] = new Bitmap(file);
threads[i] = new Thread(imgProcessing);
threads[i].Start(bt[i]);
i++;
}
foreach(Thread thread in threads)
{
thread.Join();
}
Console.WriteLine("highest snow percent:" + ((double)maxgray / sc.Length) * 100);
Console.WriteLine("lowest:" + (((double)maxwhite) / sc.Length) * 100);
imgprocessing函数:
private static void imgProcessing(Object obj1)
{
Bitmap bt = new Bitmap((Image)obj1); //exception occurs here
//Bitmap //ft = new Bitmap(bt);
Color color = new Color();
Ipn[] Iparams = new Ipn[6];
int nm = 2211840;
int i = 0;
int j = 0;
int k = 0;
//int holder = 0;
float hue = 0;
float brt = 0;
float sat = 0;
GiveId(Iparams);
for (i = 0; i < bt.Height; i++)
{
for (j = 0; j < bt.Width; j++)
{
color = bt.GetPixel(j, i);
hue = color.GetHue();
sat = color.GetSaturation();
brt = color.GetBrightness();
#region if statements
//white
if (brt > 0.7)
{
Iparams[5].Percent++;
//ft.SetPixel(j,i,Color.White);
}
//gray
else if (sat <= 0.25)
{
Iparams[4].Percent++;
//ft.SetPixel(j, i, Color.Gray);
}
//black
else if (color.R <= 15 && color.G <= 15 && color.B <= 15 && brt < 0.3)
{
Iparams[3].Percent++;
//ft.SetPixel(j, i, Color.Black);
}
//red
else if (hue > 0 && hue <= 59)
{
Iparams[0].Percent++;
//ft.SetPixel(j, i, Color.Red);
}
//green
else if (hue > 60 && hue <= 150)
{
Iparams[1].Percent++;
//ft.SetPixel(j, i, Color.Green);
}
//blue
else if (hue > 150 && hue <= 270)
{
Iparams[2].Percent++;
//ft.SetPixel(j, i, Color.Blue);
}
#endregion
}
}
for (k = 0; k < Iparams.Length; k++)
{
Iparams[k].Percent = ((float)Iparams[k].Percent / nm) * 100;
}
//bubblesort(Iparams);
lock (lockobject)
{
#region space code
//calculate the color percentages for each color
//if (Iparams[0].ID == ID.gray && Iparams[1].ID == ID.blue)
//{
// counter2++;
//}
//else if (Iparams[1].ID == ID.white && Iparams[0].ID == ID.blue)
//{
// counter1++;
//}
#endregion
#region ocean code
//if (Iparams[0].ID == ID.blue)
//{
// Interlocked.Increment(ref counter1);
//}
//else
// Interlocked.Increment(ref counter2);
//holder = (int)(Iparams[0].Percent - Iparams[1].Percent);
//Interlocked.Add(ref dis, holder);
#endregion
////ft.Save(@"C:\Users\User\Desktop\results\" + name + ".jpg");
//Interlocked.Increment(ref name);
#region veg code
//if (Iparams[0].ID == ID.green)
//{
// Interlocked.Increment(ref counter1);
// holder = (int)(Iparams[0].Percent - Iparams[1].Percent);
// Interlocked.Add(ref dis, holder);
//}
//else
// Interlocked.Increment(ref counter2);
#endregion
#region snow&clouds code
/*understanding what is the highest percentages of white and gray and the lowest.
*
*/
double graypercent = Convert.ToDouble(Iparams[4].Percent);
double whitepercent = Convert.ToDouble(Iparams[5].Percent);
if(Iparams[4].Percent > maxgray)
{
Interlocked.Exchange(ref maxgray, graypercent);
}
else if(Iparams[4].Percent < mingray)
{
Interlocked.Exchange(ref mingray, graypercent);
}
if(Iparams[5].Percent > maxwhite)
{
Interlocked.Exchange(ref maxwhite, Iparams[5].Percent);
}
else if(Iparams[5].Percent < minwhite)
{
Interlocked.Exchange(ref minwhite, Iparams[5].Percent);
}
#endregion
}