views:

98

answers:

1

I have identified this call as a bottleneck in a high pressure function.

graphics.DrawImage(smallBitmap, x , y);

Is there a faster way to blend small semi transparent bitmaps into a larger semi transparent one?

Example Usage:

XY[] locations = GetLocs();
     Bitmap[] bitmaps = GetBmps(); //small images sizes vary approx 30px x 30px

        using (Bitmap large = new Bitmap(500, 500, PixelFormat.Format32bppPArgb))
        using (Graphics largeGraphics = Graphics.FromImage(large))
        {
          for(var i=0; i < largeNumber; i++)
          {
            //this is the bottleneck
            largeGraphics.DrawImage(bitmaps[i], locations[i].x , locations[i].y); 
         }
       }

       var done = new MemoryStream(); 
       large.Save(done, ImageFormat.Png); 
       done.Position = 0;
       return (done);

The DrawImage calls take a small 32bppPArgb bitmaps and copies them into a larger bitmap at locations that vary and the small bitmaps might only partially overlap the larger bitmaps visible area. Both images have semi transparent contents that get blended by DrawImage in a way that is important to the output. I've done some testing with BitBlt but not seen significant speed improvement and the alpha blending didn't come out the same in my tests. I'm open to just about any method including a better call to bitblt or unsafe c# code.

A: 

After some testing I see that Dan was right (see comments above). It is possible to beat GDI Draw Image performance if you don't need all the blending features it offers, but in the case of transparency blends I don't think there is room for material improvement.

Glenn