将 2 个透明图像合并到图片框中

发布于 2024-10-12 11:35:59 字数 599 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(2

廻憶裏菂餘溫 2024-10-19 11:35:59

首先,您在完全相同的位置绘制图像,因此您确实面临一个图像完全覆盖另一个图像的风险。

也就是说,仅仅在图形上绘制是不够的。您还需要使图片框控件无效。这将迫使它重新绘制自己。

作为对当前程序的粗略测试,您可以最小化它,然后最大化它,看看 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.

草莓味的萝莉 2024-10-19 11:35:59

啊现在我明白我做错了什么了。

这是我修改后的代码。问题解决了。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newimage As New Bitmap(ImageList1.Images(0).Width, ImageList1.Images(0).Height)
        Dim g As Graphics = Graphics.FromImage(newimage)
        g.DrawImage(ImageList1.Images(0), 0, 0)
        g.DrawImage(ImageList1.Images(1), 0, 0)
        PictureBox1.Image = newimage
        g.Dispose()    
End Sub

Ahh now I understand what I've done wrong.

Here is my revised code. Problem solved.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newimage As New Bitmap(ImageList1.Images(0).Width, ImageList1.Images(0).Height)
        Dim g As Graphics = Graphics.FromImage(newimage)
        g.DrawImage(ImageList1.Images(0), 0, 0)
        g.DrawImage(ImageList1.Images(1), 0, 0)
        PictureBox1.Image = newimage
        g.Dispose()    
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文