不安全的C#位图洪水填充
**不安全的C#位图洪水填充,c#,bitmap,flood-fill,C#,Bitmap,Flood Fill,** 如何使用“GetPixel2”查找点的颜色 ** 所以我有一个有很多单色形状的位图。 我有这些形状的x,y点列表。然后是第二个列表 在这些点上使用预期的颜色 最后有一个算法使用位图。Getpixel和SetPixel工作。 这确实很慢。 建议使用直接内存访问来解决此问题。我想使用他们的样本,而不是在整个图像中循环,并点击一个x,y点 Bitmap bmp2 = (Bitmap)Bitmap.FromFile(Environment.CurrentDirectory + @"\Content
如何使用“GetPixel2”查找点的颜色 ** 所以我有一个有很多单色形状的位图。 我有这些形状的x,y点列表。然后是第二个列表 在这些点上使用预期的颜色 最后有一个算法使用位图。Getpixel和SetPixel工作。 这确实很慢。 建议使用直接内存访问来解决此问题。我想使用他们的样本,而不是在整个图像中循环,并点击一个x,y点
Bitmap bmp2 = (Bitmap)Bitmap.FromFile(Environment.CurrentDirectory + @"\Content\map\provinces.bmp");
BitmapData bitmapData = bmp2.LockBits(new System.Drawing.Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadWrite, bmp2.PixelFormat);
int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(bmp2.PixelFormat) / 8;
int heightInPixels = bitmapData.Height;
int widthInBytes = bitmapData.Width * bytesPerPixel;
System.Drawing.Point pt = new System.Drawing.Point((int)provpos2[0].X, (int)provpos2[0].Y);
System.Drawing.Color targetColor = System.Drawing.Color.FromArgb(255, provcolors[0].R, provcolors[0].G, provcolors[0].B);
if (!ColorMatch(GetPixel2(pt.X, pt.Y, bytesPerPixel, bitmapData), targetColor)){
// This hits the completely wrong area.
}
public System.Drawing.Color GetPixel2(int x, int y, int bytesPerPixel, BitmapData bitmapData)
{
unsafe
{
byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
x = x + bytesPerPixel;
System.Drawing.Color a = System.Drawing.Color.FromArgb(255, currentLine[x + 2], currentLine[x + 1], currentLine[x]);
return a;
}
}
public static bool ColorMatch(System.Drawing.Color a,System.Drawing.Color b)
{
return (a.ToArgb() & 0xffffff) == (b.ToArgb() & 0xffffff);
}
字节/像素在3。尝试将其更改为4,但只会命中位图上的另一个不需要的位置
- 在5632x2048位图上,它似乎达到了1023x,351y左右,而不是所需的3084x,319y李>
private Bitmap Convert565bppTo24bpp(Bitmap ConvertMe)
{
Bitmap clone = new Bitmap(ConvertMe.Width, ConvertMe.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);//.Format32bppPArgb);
using (Graphics gr = Graphics.FromImage(clone))
{ gr.DrawImage(ConvertMe, new Rectangle(0, 0, clone.Width, clone.Height)); }
return clone;
}
... 你的问题是什么?如何让“GetPixel2”在某一点上找到颜色。酷。那么也许可以把它放在你的问题里。它必须是x=x*bytesperpoixel;绝对是加号而不是乘法符号。你能把它作为一个单独的答案提交给我吗?点击错误,我想把它作为对你问题的评论。哦,也许这本书读起来更好。令人惊讶的是,这本书非常准确!正是我想要的,谢谢!实际上,使用Convert565方法或链接中的类,似乎会产生较慢的结果,并在大约500个条目之后出现内存不足崩溃。原始像素格式已经是24bppRgb.ehm了,如果不是565,那么你也不需要使用这个函数。但是您确定源输入是24bppRgb吗?。该代码是我过去使用过的代码的一部分,后来使用了一种不同的方法对其进行了改进,该方法工作得更快,需要回过头来寻找更新的版本。如果是针对相机,请确保在准备就绪时处理图像,否则图像可能会填满内存,并在几帧后崩溃。ea伪代码摄像头\u框架\u更新>>更新picturebox(框架)>>dispose(框架)Visualstudio 2015可能会检测到此类遗忘的dispose操作。