VB.NET Winforms:叠加两个透明图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20180200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:56:54  来源:igfitidea点击:

VB.NET Winforms: Overlay two transparent images

vb.netwinformsimage

提问by Ant Moore

I am attempting to overlay two transparent images within a winform, but it keeps rendering the form's background image behind the top transparent image, as opposed to the second image...

我试图在一个 winform 中覆盖两个透明图像,但它一直在顶部透明图像后面渲染表单的背景图像,而不是第二个图像......

My basic set up is I have two panels and in each panel is a picturebox. Each image in the picture boxes have some transparent areas. I've set the BackColor of the panels to color.transparent.

我的基本设置是我有两个面板,每个面板中都有一个图片框。图片框中的每个图像都有一些透明区域。我已将面板的 BackColor 设置为 color.transparent。

When I make one panel overlay the other I'm seeing the form's backcolor come through as opposed to the underlaying image.

当我让一个面板覆盖另一个面板时,我看到表单的背景颜色与底层图像相反。

Am I missing a property that I can set?

我是否缺少可以设置的属性?

回答by Blue0500

You only need one picture box. The overlay can be done with graphics.

您只需要一个图片框。覆盖可以用图形来完成。

Imports System.Drawing

Dim OverlayImage As New Bitmap("Some Path", True)
Dim BackImage As New Bitmap("Some Path", True)
g As Graphics = Graphics.FromImage(BackImage)

g.DrawImage(OverlayImage, 0, 0)
pictureBox1.Image = BackImage



如果你想让计时器移动覆盖的图像,那么首先,创建一个变量,


Dim posX As Integer = 0Dim posX As Integer = 0


然后


g.DrawImage(OverlayImage, posX, 0)g.DrawImage(OverlayImage, posX, 0)


当你的计时器滴答时使用Now,增加posXposX10

回答by CrazyTim

Here's a complete function to overlay two images (adapted from Blue0500's answer):

这是叠加两个图像的完整功能(改编自 Blue0500 的答案):

''' <summary> Return a new image with one superimposed over the other. </summary>
Function OverlayImgs(ByVal BackgroundImg As System.Drawing.Bitmap, ByVal OverlayImg As System.Drawing.Bitmap, Position As System.Drawing.Point) As System.Drawing.Bitmap
    Dim g = System.Drawing.Graphics.FromImage(BackgroundImg)
    g.DrawImage(OverlayImg, Position)
    Return BackgroundImg
End Function

Usage:

用法:

lblTest.Image = OverlayImgs(Img1, Img2, New Point(16, 16))

回答by OneFineDay

You don't need a PictureBoxfor this unless you are using it for the canvas. You can draw all images to a Rectangle structure and move them around. Personally I would create a class object that has a Rectangle, Image and other properties and hold them in a collection. Then you simply draw the class objects by there properties including location. If there images contain transparencies they will overlay for you. This also gives you a method to check for collisions via the Rectangle.IntersectsWithfunction.

PictureBox除非您将它用于画布,否则您不需要 a 。您可以将所有图像绘制到 Rectangle 结构并四处移动。我个人会创建一个具有矩形、图像和其他属性的类对象,并将它们保存在一个集合中。然后,您只需通过包括位置在内的属性绘制类对象。如果图像包含透明胶片,它们将为您叠加。这也为您提供了一种通过该Rectangle.IntersectsWith函数检查碰撞的方法。