使用透明度混合将小位图复制到大位图:什么比 Graphics.DrawImage(smallBitmap, x , y) 更快?

发布于 2024-08-26 03:58:29 字数 1008 浏览 5 评论 0原文

我已确定此调用是高压函数中的瓶颈。

graphics.DrawImage(smallBitmap, x , y);

有没有一种更快的方法将小的半透明位图混合成更大的半透明位图?

示例用法:

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);

DrawImage 调用采用较小的 32bppPArgb 位图,并将其复制到较大位图的不同位置,并且较小位图可能仅部分重叠较大位图的可见区域。两个图像都具有半透明内容,这些内容由 DrawImage 以对输出很重要的方式进行混合。我已经使用 BitBlt 进行了一些测试,但没有看到显着的速度改进,并且 alpha 混合在我的测试中表现不一样。我对任何方法都持开放态度,包括更好地调用 bitblt 或不安全的 C# 代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2024-09-02 03:58:29

经过一些测试,我发现丹是对的(见上面的评论)。如果您不需要它提供的所有混合功能,则有可能击败 GDI 绘制图像性能,但在透明度混合的情况下,我认为没有材料改进的空间。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文