如何在 Windows 10 uwp 中有效地使用 WriteableBitmap.setpixel()?

Abs*_*ith 1 c# winrt-xaml writablebitmap windows-10 uwp

我有一个场景

  • 选择一个图像文件,然后使用 BitmapDecoder 将源转换为 WriteableBitmap 并将 image.source 设置为 WriteableBitmap。
  • 现在,当用户点击图像时,我会得到坐标,然后想用特定颜色为该像素周围的整个区域着色。(就像油漆中的填充选项)。

我使用的代码是

 private void setPixelColors(int xCord, int yCord, int newColor)
 {
    Color color = bit.GetPixel(xCord, yCord);
    if (color.R <= 5 && color.G <= 5 && color.B <= 5 || newColor == ConvertColorToInt(color))
    {
        //Debug.WriteLine("The color was black or same returning");
        return;
    }
    setPixelColors(xCord + 1, yCord, newColor);
    setPixelColors(xCord, yCord + 1, newColor);
    setPixelColors(xCord - 1, yCord, newColor);
    setPixelColors(xCord, yCord - 1, newColor);
    //Debug.WriteLine("Setting the color here");
    bit.SetPixel(xCord, yCord, newColor);
 }
Run Code Online (Sandbox Code Playgroud)

这有效,但效率极低。我想知道有没有更好的方法来做到这一点。

编辑:使用库 WriteableBitmapEx。

Rob*_*SFT 5

GetPixel 和 SetPixel 扩展方法对于多次迭代更改非常昂贵,因为它们提取 BitmapContext(WriteableBitmap 的 PixelBuffer),进行更改,然后在使用 BitmapContext 完成调用时写回更新的 PixelBuffer。

WriteableBitmapEx 将在多个调用之间共享 BitmapContext,如果您先获取它并保留实时引用。仅读取 PixelBuffer 一次,进行所有更改,然后仅将其写回一次,这将显着加快。

为此,请使用 WriteableBitmapEx 的BitmapContext对象(可通过 GetBitmapContext 扩展方法访问)提取 PixelBuffer,然后根据位图上下文的需要经常调用 Get 和 SetPixel。完成后,处置 BitmapContext 以将其保存回 WriteableBitmap 的 PixelBuffer(通常通过 using 语句自动处置 BitmapContext 会更容易)。

有示例代码可以在https://github.com/teichgraf/WriteableBitmapEx的 WriteableBitmapEx GitHub 上给出总体思路

就像是:

 private void setPixelColors(int xCord, int yCord, int newColor)
 {
     using (bit.GetBitmapContext())
     {
         _setPixelColors(xCord, yCord, newColor);
     }
 }
 private void _setPixelColors(int xCord, int yCord, int newColor)
 {
    Color color = bit.GetPixel(xCord, yCord);
    if (color.R <= 5 && color.G <= 5 && color.B <= 5 || newColor == ConvertColorToInt(color))
    {
        //Debug.WriteLine("The color was black or same returning");
        return;
    }
    setPixelColors(xCord + 1, yCord, newColor);
    setPixelColors(xCord, yCord + 1, newColor);
    setPixelColors(xCord - 1, yCord, newColor);
    setPixelColors(xCord, yCord - 1, newColor);
    //Debug.WriteLine("Setting the color here");
    bit.SetPixel(xCord, yCord, newColor);
 }
Run Code Online (Sandbox Code Playgroud)

这应该使整体速度合理,但是(正如 Alex 建议的那样)您应该研究非递归洪水填充算法,特别是如果您有大的位图。递归算法将溢出堆栈以进行大填充。维基百科上有一些相当简单的选项:https : //en.wikipedia.org/wiki/Flood_fill#Alternative_implementations一个简单的方法是保持与此处基本相同的结构,但不是递归处理每个新像素,而是明确处理要编辑的像素堆栈。如果你知道你的目标是小区域,那么它本身可能就足够快了。为了支持更大的,您可能需要进一步优化。