并发Graphics.RotateTransform操作的性能不佳

时间:2012-01-07 00:13:33

标签: c# .net system.drawing

为什么在跨多个线程运行并发Graphics.RotateTransform操作时,我看不到显着的速度提升?

此示例演示了边际性能提升:

    static Bitmap rotate(Bitmap bitmap, float angle)
    {
        Bitmap rotated = new Bitmap(bitmap.Width, bitmap.Height);
        using (Graphics g = Graphics.FromImage(rotated))
        {
            g.TranslateTransform((float)bitmap.Width / 2, (float)bitmap.Height / 2);
            g.RotateTransform(angle);
            g.TranslateTransform(-(float)bitmap.Width / 2, -(float)bitmap.Height / 2);
            g.DrawImage(bitmap, new Point(0, 0));
        }
        return rotated;
    }

    static void Main(string[] args)
    {
        WebClient client = new WebClient();
        string fileName = Path.GetTempFileName();
        client.DownloadFile("http://i.imgur.com/Vwiul.png", fileName);
        Bitmap original = new Bitmap(fileName);

        DateTime start = DateTime.Now;
        rotateSingleThread(original);
        TimeSpan length = DateTime.Now - start;
        Console.WriteLine(string.Format("Single thread rotate took {0} ms", (int)length.TotalMilliseconds));

        start = DateTime.Now;
        rotateMultiThread(original);
        length = DateTime.Now - start;
        Console.WriteLine(string.Format("Multiple thread rotate took {0} ms", (int)length.TotalMilliseconds));

        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }

    static void rotateSingleThread(Bitmap original)
    {
        for (float a = 0; a < 360; a += 0.1F)
        {
            Bitmap rotated = rotate(original, a);
            rotated.Dispose();
        }
    }

    static void rotateMultiThread(Bitmap original)
    {
        int threadCount = Environment.ProcessorCount;
        int groupSize = 360 / threadCount;
        Thread[] threads = new Thread[threadCount];
        Bitmap[] bitmaps = new Bitmap[threadCount];
        for (int x = 0; x < threadCount; x++)
        {
            bitmaps[x] = new Bitmap(original);
            threads[x] = new Thread(delegate(object i)
            {
                int min = (int)i * groupSize; int max = min + groupSize;
                if ((int)i == threadCount - 1) max = 360;
                for (float a = min; a < max + 1; a+= 0.1F)
                {
                    Bitmap rotated = rotate(bitmaps[(int)i], a);
                    rotated.Dispose();
                }
            });
            threads[x].Start(x);
        }
        for (int x = 0; x < threadCount; x++) threads[x].Join();
    }

如何提高并发Graphics.RotateTransform操作的速度呢?

2 个答案:

答案 0 :(得分:1)

问题是那些GDI +调用阻塞每个进程(不是每个线程),因此即使线程并行运行,对graphics....的调用也会导致其他线程阻塞。这意味着它几乎连续处理。

请参阅下面问题中的各种答案以获取解决方法,其中最重要的是建议创建单独的流程。看起来你可能最终只是创建一个帮助命令行实用程序或其他东西。您甚至可以将stdin和stdout挂钩,这样您就可以通过IO操作传递图像进行旋转并返回新图像。

Parallelizing GDI+ Image Resizing .net

答案 1 :(得分:0)

我没有答案,但有些想法可能有所帮助:

  1. 你的线程是在多个处理器上运行的吗?
  2. 你是如何衡量表现的?请记住,UI渲染始终是 单线程。