0

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.

Glenn
  • 1,234
  • 2
  • 19
  • 33
  • 1
    I actually don't think so (not without utilizing hardware acceleration; maybe DirectX could help you here). I've been toying around with a similar problem. I found that I could write a faster DrawImage in assembler when I painted semi transparent images onto an *opaque* image, because I could cheat a little bit then, but not otherwise. – Dan Byström Mar 19 '10 at 09:56
  • I was afraid of that. I think DirectX is probably out for me because the code runs on a web server. Did you try blending using bitwise operators on unsafe int arrays? That was the one thing I'm considering trying but it would be a fair amount of work to set it up to test with my use case and I'd love to avoid building that test case if you have tried it. Thanks for the feedback. – Glenn Mar 19 '10 at 12:32
  • Dan, After some testing, I think you're right. If you post your comment below as an answer I'll mark it as accepted. – Glenn Mar 22 '10 at 15:05

1 Answers1

0

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
  • 1,234
  • 2
  • 19
  • 33