本文介绍了C#TPL在高度方向上的投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这样的代码:

I have written code like this:

private static unsafe Point[] Hproject(Bitmap bmp)
       {
           int height = bmp.Height;
           int width = bmp.Width;
           Point[] result = new Point[height];

           var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
           var startPtr = (PixelColor*)data.Scan0.ToPointer();

           Parallel.ForEach(Partitioner.Create(0, height), (h) =>
           {
               var ptr = startPtr + h.Item1 * width;

               int lBlackNumber = new int();
               for (int y = h.Item1; y < h.Item2; y++)
               {
                   lBlackNumber = 0;
                   for (int x = 0; x < width; x++)
                   {
                       var c = *ptr;
                       if (c.Blue == 255)
                       {
                           lBlackNumber++;
                       }

                       ptr++;
                   }
                   result[y].X = lBlackNumber;
               }

           });
           bmp.UnlockBits(data);
           return result;
       }


它工作正常.但是我发现很难在位图的高度方向上编写代码.能帮帮我吗?
我写了一个,但是不好.


it work well.but I find that it''s hard to written code in bitmap''s height direction.can you help me?
I write one,but it''s bad.

private static unsafe Point[] VProjectByParallelForEach(Bitmap bmp)
        {
            int height = bmp.Height;
            int width = bmp.Width;
            Point[] result = new Point[height];
            var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            var startPtr = (PixelColor*)data.Scan0.ToPointer();
            result = VProjectParallelForEach(startPtr, width, height);
            bmp.UnlockBits(data);

            return result;
        }
        private static unsafe Point[] VProjectParallelForEach(PixelColor* startPtr, int width, int height)
        {
            Point[] result = new Point[width];
            Parallel.ForEach(Partitioner.Create(0, width), (w) =>
            {
                var ptr = startPtr + w.Item1 * height;

                int lBlackNumber = new int();
                for (int x = w.Item1; x < w.Item2; x++)
                {
                    lBlackNumber = 0;
                    for (int y = 0; y < height; y++)
                    {
                        var c = *ptr;
                        if (c.Blue == 255)
                        {
                            lBlackNumber++;
                        }

                        ptr++;
                    }
                    result[x].X = lBlackNumber;
                }

            });
            return result;
        }

推荐答案


这篇关于C#TPL在高度方向上的投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:21