简体   繁体   English

在.NET中搜索数组以匹配颜色的最快方法

[英]Fastest method to search an array for matching color in .NET

I am using GetPixel from a bitmap (using LockBits so no efficiency issue there) and need to search an array of colors for the location of the color within the array as fast as possible. 我正在从位图使用GetPixel(使用LockBits,因此那里没有效率问题),并且需要尽快在颜色数组中搜索颜色在数组中的位置。 I have very large bitmaps to iterate through. 我有很大的位图要遍历。

...                
prevColor = Color.FromArgb(255, r, g, b);

int count = 1536;
for (int i = 0; i < count; i++)
{
   if (prevColor == theColorScale[i])
   {
       loc = i;
       break;
   }
}

Any suggestions? 有什么建议么?

Accepting the question as stated 接受陈述的问题

A Dictionary has O(1) lookup 字典具有O(1)查找

Dictionary<color, Int32> colors = new Dictionary<color, Int32>();
...
loc = colors[prevColor];

The key (color) in the Dictionary must be unique 词典中的键(颜色)必须唯一

Could you use a List and search it using .IndexOf()? 您可以使用列表并使用.IndexOf()搜索吗?

...
List<Color> colorScale;
/* add colors */
loc = colorScale.IndexOf(prevColor);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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