Copy Small Bitmaps on to Large Bitmap with Transparency Blend: What is faster than graphics.DrawImag

Posted by Glenn on Stack Overflow See other posts from Stack Overflow or by Glenn
Published on 2010-03-19T05:50:46Z Indexed on 2010/04/09 17:43 UTC
Read the original article Hit count: 304

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about graphics