Racket中如何为位图设置alpha值

4

所以,我正在使用该函数将位图图像分配给一个对象:

(define (make-enemy-alienship bitmap-target)
  (let ((dc (new bitmap-dc% [bitmap bitmap-target])))
    (send dc draw-bitmap (read-bitmap "alien.bmp") 0 0)))

我希望图片中的白色像素是透明的,但是文档并没有提供太多帮助。是否应该使用掩码参数?另外,如何确定哪个位图将在顶部?例如,如果我想要一个背景图像,我显然希望它在底部。

提前感谢。

1个回答

2
如果我理解正确的话,您的位图不包含透明通道,您想使用白色值作为透明度。 有一个更好的解决方案,但如果您不想修改图像文件(并坚持使用没有阿尔法通道的.bmp),您可以使用get-argb-pixelsset-argb-pixels来在像素为白色(255, 255, 255)时将阿尔法通道修改为1。
另一种可能性是将您的图像保存为 png 格式,同时将背景颜色设置为白色(顺便说一句,我建议使用不常见的颜色,比如紫色,否则您将无法在图像中使用白色像素)。这可以在任何体面的图像编辑器中完成。然后,您可以使用 png/maskread-bitmapkind 参数中加载带有“掩码”的 bitmap%
另一种方法是我推荐的方法,即使用具有 alpha 通道的图像文件,并使用 bmp/alpha(适用于 .bmp 文件)加载它。通常使用带有 alpha 通道的 png 格式(png 文件类似于 bmp 文件,但经过无损压缩)。现在,您可以使用任何透明度值,例如 50%,这很好地避免了粗糙的边缘。
关于哪个图像会在顶部,(send dc draw-bitmap bmp)始终会将bmp绘制在dc中已经绘制的内容之上,因此您在此处使用的方式是正确的。
旁注:
  • You should not call read-bitmap inside make-enemy-alienship, because calling read-bitmap is costly (it opens a file, allocates some memory buffer, copies the file in the buffer, and closes the file). Instead, you should save the result of (read-bitmap "alien.bmp") into a variable:

    (define alien-bmp (read-bitmap "alien.bmp"))
    (define (make-enemy-alienship bitmap-target)
      (let ((dc (new bitmap-dc% [bitmap bitmap-target])))
        (send dc draw-bitmap alien-bmp 0 0)))
    
  • You should use define-runtime-path to avoid problems of relative paths depending on your current directory. But this is not your prior concern and you can deal with that later, and focus for now on having nice spaceships on the screen.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接