快速将8位位图分成8个不同的1位位图

时间:2015-08-09 12:01:55

标签: c# bitmap

我需要一种方法将1000+ 8位位图转换为8位1位图。

目前我正在运行两个循环,它从主图像中读取每个像素并为其分配一个1bpp图像。这需要很长时间才能完成,无论如何更好吗?

以下是我的代码示例(仅分为两个图像):

Bitmap rawBMP = new Bitmap(path);
Bitmap supportRAW = new Bitmap(rawBMP.Width, rawBMP.Height);
Bitmap modelRAW = new Bitmap(rawBMP.Width, rawBMP.Height);

Color color = new Color();           

        for (int x = 0; x < rawBMP.Width; x++)
        {
            for (int y = 0; y < rawBMP.Height; y++)
            {
                color = rawBMP.GetPixel(x, y);

                if (color.R == 166) //model
                {
                    modelRAW.SetPixel(x, y, Color.White);
                }

                if (color.R == 249) //Support
                {
                    supportRAW.SetPixel(x, y, Color.White);
                }
            }
        }
        var supportBMP = supportRAW.Clone(new Rectangle(0, 0, rawBMP.Width, rawBMP.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
        var modelBMP = modelRAW.Clone(new Rectangle(0, 0, rawBMP.Width, rawBMP.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);

1 个答案:

答案 0 :(得分:0)

如果你必须检查每个像素,那么你将不得不至少一次遍历它们,但是像TaW建议有更有效的方法来访问像素。

SetPixel和GetPixel比直接访问数据慢得多,查看使用unsafe直接访问数据,或编组来回复制数据。 (请参阅https://stackoverflow.com/a/1563170以获取notJim撰写的更详细信息)

相关问题