Winforms半透明PNG超过半透明PNG [英] Winforms semi-transparent PNG over semi-transparent PNG

查看:104
本文介绍了Winforms半透明PNG超过半透明PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我一定缺少明显的东西,但是经过几个小时的搜索后我找不到.是否无法使用PictureBox或其他控件包含具有部分透明/alpha混合像素的图像,并将其放置在另一图像上,并使混合基于其下的图像?

I think I must be missing something obvious, but I'm unable to find this after several hours of searching. Is there no way to use a PictureBox or other control to contain an image with partial transparent/alpha-blended pixels, and place that over another image and have the blending be based on the image under it?

例如,这将产生我想要的结果:

For example, this produces the results I want:

  1. 在面板上放置一个面板.
  2. 添加OnPaint处理程序.
  3. 在OnPaint处理程序中,绘制1个PNG,然后在其上绘制另一个PNG,两者均使用Graphics.DrawImage.

这不是:

  1. 将PictureBox放置在窗体上并将其设置为PNG.

  1. Place a PictureBox on a form and set it to a PNG.

在窗体上放置另一个PictureBox并将其设置为PNG.

Place another PictureBox on the form and set it to a PNG.

将第二个图片框放在第一个图片框上.

Place the 2nd picture box over the first.

...即使第二个图片框只是空的并且背景颜色为透明",它仍然会覆盖下面的图片.

...even if the 2nd picture box is just empty and has a background color of Transparent, it still covers the picture below it.

我已经读过这篇文章,原因是所有winform控件都是Windows,因此从本质上讲它们不是透明的.

I've read this stems from all winform controls being windows, so by nature they aren't transparent.

...但是,即使是我要迁移的具有15年历史的平台Borland的VCL,也具有多个无窗口控件,因此很难想象Winform至少没有一些简单的解决方案?

...but even the 15 year old platform I'm migrating from, Borland's VCL, had several windowless controls, so it's hard to imaging winforms doesn't at least have some easy solution?

我在上面的第一个示例是一个正确的答案,但是当您只能使用一个大面板并在其中绘制所有控件"时,这会增加很多工作.如果您可以使用带有单独的鼠标事件/等的单独控件,那就更好了.即使不是图像控件,也没有我必须自己绘制的控件,这很好,只要我可以在每个控件中放置一张图像即可.在VCL中,他们将其称为绘画框",只是可以放置在表单上并在其上绘制任何内容的矩形区域.有它自己的鼠标事件,边界等.如果您未在其中绘制任何内容,则除了它仍会获得鼠标事件之外,它甚至不存在(100%透明),因此可以用作热点".点"或目标".

My first example above is one answer, true, but that adds a lot of work when you can only use one big panel and draw all of your "controls" inside of it. Much nicer if you can have separate controls with separate mouse events/etc. Even if not an image control, and a control I have to draw myself, that would be fine, as long as I can just put one image in each control. In VCL they called this a "paint box", just a rectangle area you could place on a form and draw whatever you want on it. Has it's own mouse events, Bounds, etc. If you don't draw anything in it, it is like it's not even there (100% transparent) other than the fact it still gets mouse events, so can be used as a "hot spot" or "target" as well.

推荐答案

PictureBox控件很好地支持透明度,只需将其BackColor属性设置为Transparent.这将使其父对象的像素作为背景可见.

The PictureBox control supports transparency well, just set its BackColor property to Transparent. Which will make the pixels of its Parent visible as the background.

问题在于,设计人员不允许您将第二个图片框作为第一个图片框的孩子.您只需要在构造函数中使用少量代码即可重新对其进行父化.并为其指定一个新位置,因为它是相对于父级的相对位置.像这样:

The rub is that the designer won't let you make the 2nd picture box a child of the 1st one. All you need is a wee bit of code in the constructor to re-parent it. And give it a new Location since that is relative from the parent. Like this:

    public Form1() {
        InitializeComponent();
        pictureBox1.Controls.Add(pictureBox2);
        pictureBox2.Location = new Point(0, 0);
        pictureBox2.BackColor = Color.Transparent;
    }

请不要犹豫使用OnPaint().

Don't hesitate to use OnPaint() btw.

这篇关于Winforms半透明PNG超过半透明PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆