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

查看:34
本文介绍了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. 在表单上放置一个图片框并将其设置为 PNG.

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

在表单上放置另一个图片框并将其设置为 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 控件都是窗口,所以本质上它们不透明.

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

...但即使是我迁移过来的 15 年历史的平台,Borland 的 VCL,也有几个无窗口控件,所以很难想象 winforms 至少没有一些简单的解决方案吗?

...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.这将使其 Parent 的像素作为背景可见.

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天全站免登陆