简体   繁体   English

如何在32位位图图像的上方和下方添加透明像素

[英]how to add transparent pixels above and below a 32 bit bitmap images

I need to add at runtime to an inmemory image 15 lines of transparent pixels on top and 20 on the bottom. 我需要在运行时向内存图像中添加15行透明像素(顶部)和20行底部(底部)。

The images are loaded in a TcxImagelist (from DevExpress Express Library), they can be therefore retrieved as 32bit bitmaps. 图像被加载到TcxImagelist(来自DevExpress Express库)中,因此可以将它们检索为32位位图。

If the image is 400x75 after the manipulation should be 400x(75+15+20) = 400x110 如果图像在处理后为400x75,则应为400x(75 + 15 + 20)= 400x110

How to perform this task? 如何执行此任务?

There is no such a thing as "transparent" pixels. 没有“透明”像素之类的东西。 All you can do is tag them for the renderer so that it will know they aren't supposed to be displayed. 您所能做的就是为渲染器标记它们,以便它知道不应显示它们。 here are the 3 most common ways of tagging but which one you use depends on when you're doing for rendering: 这是最常见的3种标记方式,但您使用哪种标记取决于您进行渲染的时间:

  • Use a transparency map: a second pixmap which indicates the "level" of transparency of each pixel. 使用透明度图:第二个像素图,它指示每个像素的透明度“级别”。 The rendered then uses that as a weighting value to combine the top and bottom layers in the final color. 然后,渲染器将其用作加权值,以合并最终颜色中的顶层和底层。 If you just want binary transparency (opaque/transparent), you can use a bitmap and use a simple XOR on each pixel which makes it very fast. 如果只需要二进制透明度(不透明/透明),则可以使用位图,并对每个像素使用简单的XOR,这样可以非常快。
  • Define a "transparent color". 定义“透明颜色”。 You can then XOR it with the transparent color and the bottom layer. 然后,您可以将其与透明色和底层进行XOR。 Also very fast and doesn't require any additional storage. 也非常快,不需要任何额外的存储。 It does have some side effects, though (one color cannot be used in the top layer image, for instance) 但是,它确实有一些副作用(例如,不能在顶层图像中使用一种颜色)
  • use the last byte of the 32-bits bitmap as the transparency level (alpha channel). 使用32位位图的最后一个字节作为透明级别(alpha通道)。 In effect, you store the transparency map (255 distinct levels of transparency) with the image. 实际上,您将透明度贴图(255个不同级别的透明度)与图像一起存储。

Now, in your case, since you seem to be only copying a rectangle over a rectangle, another aproach that would be: create a canvas of the same size as the final image, copy the inferior rectangle on it and the draw the to layer on top of it. 现在,在您的情况下,由于您似乎只是在矩形上复制一个矩形,因此另一个方法是:创建一个与最终图像大小相同的画布,在其上复制下一个矩形,并在其上绘制to层顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM