Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不安全的C#位图洪水填充_C#_Bitmap_Flood Fill - Fatal编程技术网

不安全的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
不完全确定为什么它不适合你,但请记住:

每像素位数来自所使用的颜色格式。有几种格式,有些比其他格式更方便,有时需要将它们转换为严格的RGB格式。ea每个颜色通道8位,也有RGBA,还有一些相机使用的按位565表示法的RGB,每种颜色有24位。有些格式在winforms中不受支持,但在基于wpf的应用程序中受支持,如16位灰色格式。(因为wpf更像是新时代的设计友好型)

也许可以试试这个,它对我很有用:

如果是565的话,你可以这样做

     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操作。