C# 在null c中从有效对象到位图的转换#
我试图使用线程来处理多个相互平行的图像。在代码中,您将看到我为“imgprocessing”方法提供了一个位图,但图像处理只接收对象参数。所以我必须键入cast-to-bitmap,在某个点它抛出异常:“参数无效”。显然,bitmap在某个点变为null。 只有在几个线程已经完成处理之后,才会发生这种情况。 问题是:为什么在类型转换期间我的位图为空?我不知道为什么它会停止正常转换。 代码- 主要内容: IMG处理功能:C# 在null c中从有效对象到位图的转换#,c#,image-processing,bitmap,null,C#,Image Processing,Bitmap,Null,我试图使用线程来处理多个相互平行的图像。在代码中,您将看到我为“imgprocessing”方法提供了一个位图,但图像处理只接收对象参数。所以我必须键入cast-to-bitmap,在某个点它抛出异常:“参数无效”。显然,bitmap在某个点变为null。 只有在几个线程已经完成处理之后,才会发生这种情况。 问题是:为什么在类型转换期间我的位图为空?我不知道为什么它会停止正常转换。 代码- 主要内容: IMG处理功能: private static void imgProcessing(Obje
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
}
私有静态无效imgProcessing(对象obj1)
{
位图bt=新位图((图像)obj1);//此处发生异常
//位图//英尺=新位图(bt);
颜色=新颜色();
Ipn[]IPARMS=新Ipn[6];
int nm=2211840;
int i=0;
int j=0;
int k=0;
//int holder=0;
浮动色调=0;
浮动brt=0;
浮动sat=0;
GiveId(ipaams);
对于(i=0;i0.7)
{
iParms[5]。百分比++;
//ft.SetPixel(j,i,颜色.白色);
}
//灰色的
否则如果(sat)您能否简化您的示例,使其能够在本地实际重现问题?例如,如果您在错误行之后从imgProcessing方法中删除所有代码,您仍然能够重现问题?如果您不从磁盘加载位图,而是动态创建一组位图,会发生什么情况文件为空?位图(字符串)构造函数仅为未找到的文件指定了一个异常。顺便说一句,我相信您有一个瓶颈。位图(字符串)构造函数阻塞I/O,所以您一次只读取一个文件,等待I/O,启动一个线程,然后继续循环的下一个迭代。为什么不将文件名传递给线程,让线程启动I/O,这样循环就不会阻塞?chris rollins-感谢您对瓶颈的提醒,但我没有告诉您完全理解“I/O块”的含义。如果我传递线程文件名并在imgprocessing中创建位图,这不是因为读取冲突吗?只有当他们试图读取相同的文件时才应该是问题。但是,如果我记得正确,GDI(System.Drawing使用的)无论如何,不允许多个线程在同一位图对象上读/写。
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
}