C# 循环通过不包括区域的像素
我有以下用于在图像中循环像素的代码:C# 循环通过不包括区域的像素,c#,C#,我有以下用于在图像中循环像素的代码: Bitmap myBitmap = new Bitmap(pictureBox1.Image); for (x = 0; x < pictureBox1.Image.Width; x += 1) { for (y = 0; y < pictureBox1.Image.Height; y += 1) { Color pixelColor = myBitmap.GetPixel(x, y);
Bitmap myBitmap = new Bitmap(pictureBox1.Image);
for (x = 0; x < pictureBox1.Image.Width; x += 1)
{
for (y = 0; y < pictureBox1.Image.Height; y += 1)
{
Color pixelColor = myBitmap.GetPixel(x, y);
aws = pixelColor.GetBrightness();
}
}
我如何循环所有像素,除了我上面写的那个?
谢谢大家! 可能编写一个函数来测试x和y,如下所示:
public static bool IsInArea(int testx, int testy, int x1, int y1, int x2, int y2)
{
if (testx > x1 && testx < x2 && testy > y1 && testy < y2)
{
return true;
}
else
{
return false;
}
}
publicstaticboolsinarea(inttestx,inttesty,intx1,inty1,intx2,inty2)
{
if(testx>x1&&testxy1&&testy
然后像这样使用它:
BitmapData bmd = myBitmap.LockBits(new Rectangle(0, 0, myBitmap.Width, myBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
myBitmap.PixelFormat);
int PixelSize = 4;
unsafe
{
for (int y = 0; y < bmd.Height; y++)
{
byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);
for (int x = 0; x < bmd.Width; x++)
{
if (!IsInArea(x, y, 200, 30, 250, 30))
{
int b = row[x * PixelSize]; //Blue
int g = row[x * PixelSize + 1]; //Green
int r = row[x * PixelSize + 2]; //Red
int a = row[x * PixelSize + 3]; //Alpha
Color c = Color.FromArgb(a, r, g, b);
float brightness = c.GetBrightness();
}
}
}
}
myBitmap.UnlockBits(bmd);
BitmapData bmd=myBitmap.LockBits(新矩形(0,0,myBitmap.Width,myBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
像素格式);
int PixelSize=4;
不安全的
{
对于(int y=0;y
可能编写一个函数来测试x和y,如下所示:
public static bool IsInArea(int testx, int testy, int x1, int y1, int x2, int y2)
{
if (testx > x1 && testx < x2 && testy > y1 && testy < y2)
{
return true;
}
else
{
return false;
}
}
publicstaticboolsinarea(inttestx,inttesty,intx1,inty1,intx2,inty2)
{
if(testx>x1&&testxy1&&testy
然后像这样使用它:
BitmapData bmd = myBitmap.LockBits(new Rectangle(0, 0, myBitmap.Width, myBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
myBitmap.PixelFormat);
int PixelSize = 4;
unsafe
{
for (int y = 0; y < bmd.Height; y++)
{
byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);
for (int x = 0; x < bmd.Width; x++)
{
if (!IsInArea(x, y, 200, 30, 250, 30))
{
int b = row[x * PixelSize]; //Blue
int g = row[x * PixelSize + 1]; //Green
int r = row[x * PixelSize + 2]; //Red
int a = row[x * PixelSize + 3]; //Alpha
Color c = Color.FromArgb(a, r, g, b);
float brightness = c.GetBrightness();
}
}
}
}
myBitmap.UnlockBits(bmd);
BitmapData bmd=myBitmap.LockBits(新矩形(0,0,myBitmap.Width,myBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
像素格式);
int PixelSize=4;
不安全的
{
对于(int y=0;y
我认为您可以执行以下操作
Rectangle rectToExclude = new Rectangle(200, 30, 250, 30);//rectangle to be excluded
Bitmap myBitmap = new Bitmap(pictureBox1.Image);
for (x = 0; x < pictureBox1.Image.Width; x += 1)
{
for (y = 0; y < pictureBox1.Image.Height; y += 1)
{
if (rectToExclude.Contains(x, y)) continue; // if in the exclude rect then continue without doing any effects
Color pixelColor = myBitmap.GetPixel(x, y);
aws = pixelColor.GetBrightness();
}
}
Rectangle rectToExclude=新矩形(200,30,250,30)//要排除的矩形
位图myBitmap=新位图(pictureBox1.Image);
对于(x=0;x
我认为您可以执行以下操作
Rectangle rectToExclude = new Rectangle(200, 30, 250, 30);//rectangle to be excluded
Bitmap myBitmap = new Bitmap(pictureBox1.Image);
for (x = 0; x < pictureBox1.Image.Width; x += 1)
{
for (y = 0; y < pictureBox1.Image.Height; y += 1)
{
if (rectToExclude.Contains(x, y)) continue; // if in the exclude rect then continue without doing any effects
Color pixelColor = myBitmap.GetPixel(x, y);
aws = pixelColor.GetBrightness();
}
}
Rectangle rectToExclude=新矩形(200,30,250,30)//要排除的矩形
位图myBitmap=新位图(pictureBox1.Image);
对于(x=0;x
if(x>200&&UsingGetPixel
会非常慢,尤其是在图像较大的情况下..使用指针..或者if(x>200&&UsingGetPixel
会非常慢,尤其是在图像较大的情况下..使用指针..或者我可以建议您在if语句中返回测试吗?应该节省一些计算机周期return(test1和test2等);
是的,你是对的,但我会让Theodoros80来实现它好的,我没有测试它…这只是给你一个想法如何完成你需要的…@Theodoros80-这将比yazan的答案更有效,因为它一次获得所有位图像素,而不是一个像素。我建议你只在if语句中返回测试?应该可以节省一些计算机周期return(test1和test2等);
是的,你是对的,但我会让Theodoros80来实现它好的,我没有测试它…这只是给你一个想法如何完成你需要的…@Theodoros80-这将比yazan的答案更有效,因为它一次获得所有位图像素,而不是一个像素。哇!如此简单和有效!!!我想我也可以使用其他形状,而不是矩形。谢谢所有其他人或贡献谢谢你,亚赞。哇!太简单了!我想我也可以用其他形状,除了矩形。谢谢所有其他的或有贡献的。谢谢亚赞。