C# 如何使用以下方法确定图像是否包含特定颜色?
我需要确定图像是否包含特定颜色:C# 如何使用以下方法确定图像是否包含特定颜色?,c#,.net,system.drawing,C#,.net,System.drawing,我需要确定图像是否包含特定颜色: r:255 g:0 b:192 我发现了这个,但是如果图像包含上述颜色,我需要返回一个布尔值,而不是返回点 public static List<Point> FindAllPixelLocations(this Bitmap img, Color color) { var points = new List<Point>(); int c = color.ToArgb(
r:255
g:0
b:192
我发现了这个,但是如果图像包含上述颜色,我需要返回一个布尔值,而不是返回点
public static List<Point> FindAllPixelLocations(this Bitmap img, Color color)
{
var points = new List<Point>();
int c = color.ToArgb();
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
if (c.Equals(img.GetPixel(x, y).ToArgb())) points.Add(new Point(x, y));
}
}
return points;
}
public static List FindAllPixelLocations(此位图img,彩色)
{
var points=新列表();
int c=color.ToArgb();
对于(int x=0;x
听起来你只需要更换
if (c.Equals(img.GetPixel(x, y).ToArgb())) points.Add(new Point(x, y));
借
听起来你只需要换一个
if (c.Equals(img.GetPixel(x, y).ToArgb())) points.Add(new Point(x, y));
借
大概是这样的:
public static bool HasColor(this Bitmap img, Color color)
{
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
if (img.GetPixel(x, y) == color)
return true;
}
}
return false;
}
publicstaticboolhascolor(此位图img,Color)
{
对于(int x=0;x
类似这样的内容:
public static bool HasColor(this Bitmap img, Color color)
{
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
if (img.GetPixel(x, y) == color)
return true;
}
}
return false;
}
publicstaticboolhascolor(此位图img,Color)
{
对于(int x=0;x
确切地说,这两个答案都是正确的,但您必须知道GetPixel(x,y)
和SetPixel(x,y)
颜色非常慢,对于高分辨率的图像来说是不好的
为此,您可以在此处使用LockBitmap
类:
这大约快了5倍。确切地说,这两个答案都是正确的,但您必须知道
GetPixel(x,y)
和SetPixel(x,y)
颜色非常慢,对于高分辨率的图像来说是不好的
为此,您可以在此处使用LockBitmap
类:
这大约快了5倍。您可能希望将函数类型更改为bool,这样就不会结束检查列表。您可能希望将函数类型更改为bool,这样就不会结束检查列表。