将 2 个透明图像合并到图片框中
我正在尝试使用 GDI+ 合并两个具有透明度的 PNG,并将其存储在 ImageList 中,然后将其显示在 PictureBox 中。我的 PictureBox 大小、第一张图像大小和第二张图像大小都是相同的。我认为这很简单,但以下代码不起作用,我不知道为什么。我进行了搜索,但找不到任何专门处理 PictureBox 的代码片段。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = Nothing
Dim myGraphic As Graphics = PictureBox1.CreateGraphics
myGraphic.DrawImageUnscaled(ImageList1.Images(0), 0, 0)
myGraphic.DrawImageUnscaled(ImageList1.Images(1), 0, 0)
End Sub
有谁知道我缺少什么?当我单击按钮时,我看到 PictureBox 的图像瞬间闪烁。
提前致谢。
I am trying to use GDI+ to merge two PNG's with transparency that I have stored in an ImageList, and then show this in a PictureBox. My PictureBox size, first image size, and second image size are all the same. I thought this would be simple but the following code does not work and I am not sure why. I have searched but I could not find any code snippets specifically dealing with PictureBoxes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = Nothing
Dim myGraphic As Graphics = PictureBox1.CreateGraphics
myGraphic.DrawImageUnscaled(ImageList1.Images(0), 0, 0)
myGraphic.DrawImageUnscaled(ImageList1.Images(1), 0, 0)
End Sub
Does anyone know what I am missing? At the moment when I click the button I see the PictureBox flicker with the image for a splitsecond.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您在完全相同的位置绘制图像,因此您确实面临一个图像完全覆盖另一个图像的风险。
也就是说,仅仅在图形上绘制是不够的。您还需要使图片框控件无效。这将迫使它重新绘制自己。
作为对当前程序的粗略测试,您可以最小化它,然后最大化它,看看 PNG 是否存在。这也将强制重绘图片框。
First of all, you're drawing the images in the exact same location so you do risk that one image is completely overwriting the other.
That said, it's not enough just to draw on the graphic. You need to invalidate the picture box control too. This will force it to redraw itself.
As a crude test with your current program, you can minimize it and then maximize it and see if the PNGs are there. This will also force-redraw the picturebox.
啊现在我明白我做错了什么了。
这是我修改后的代码。问题解决了。
Ahh now I understand what I've done wrong.
Here is my revised code. Problem solved.