如何使 PictureBox 真正透明

发布于 2024-12-23 17:36:11 字数 592 浏览 3 评论 0原文

我有一个 PictureBox,可以垂直移动。 PictureBox 中显示的图像是透明的 GIF,因此在图像查看器中查看时,它没有背景。

问题是,当我在应用程序中移动 PictureBox 时,PictureBox 的背景移动得太奇怪 - 几乎就像 PictureBox 本身有背景一样。

之前:

在此处输入图像描述

之后(移动时):

在此处输入图像描述

一些代码:

path = "C:\\note.gif";
note.Image = Image.FromFile(path);
note.Visible = true;
note.BackColor = Color.Transparent;
panel.Controls.Add(note);

我也尝试过使图片框双缓冲,但是那也行不通。

I've got a PictureBox, which can be moved vertically. The image displayed in the PictureBox is a transparent GIF, so when viewed in an image viewer, it has no background.

The problem is that when I move the PictureBox in the application, the PictureBox's background moves around too strangely - almost as if the PictureBox has a background itself.

Before:

enter image description here

After (while moving):

enter image description here

Some code:

path = "C:\\note.gif";
note.Image = Image.FromFile(path);
note.Visible = true;
note.BackColor = Color.Transparent;
panel.Controls.Add(note);

I've also tried making the picturebox double buffered, but that doesn't work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

自在安然 2024-12-30 17:36:11

虽然 WinForms 不太适合用户控件的透明性,但这是可能的。请参阅此处本文。在其中,作者建议从 Panel 而不是 UserControl 派生,并重写 OnPaintBackground 方法以不执行任何操作。这将阻止您绘制背景。

protected override void OnPaintBackground(PaintEventArgs pevent)
{
    //do nothing
}

protected override void OnMove(EventArgs e)
{
    RecreateHandle();
}

// Override the CreateParams property:
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle = 0x00000020; //WS_EX_TRANSPARENT
        return cp;
    }
}

最后,覆盖 OnPaint 函数,您可以绘制图片框。

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;

    //Do your drawing here
}

使用它,您可以创建一个具有透明度的自定义图片框,但请注意,如果您在屏幕上实时移动它,您会感到闪烁和模糊。

使用此技术和类似的技术,我们成功获得了一个 WinForms 应用程序,Premex XPort使用与其网站类似的品牌进行渲染。这涉及多个透明控件、绘画技巧以及各种使其正确显示的方法。

总之,Winforms 在这方面表现不佳的原因是在基于 Win32 的技术中,一个控件在屏幕上拥有一个像素。无法像您在 HTML 或 WPF 中所期望的那样真正地合成具有透明度的像素。后来的 Windows 技术 (WPF) 在这方面做得特别好,因此如果您确实希望在应用程序中大量使用透明度,我建议至少部分地迁移到此平台(WPF 可以托管在 WinForms 中,反之亦然)。

此致,

While WinForms is poorly suited to transparency in usercontrols, it is possible. See this article here. In it the author suggests deriving from Panel rather then UserControl and overridding the OnPaintBackground method to do nothing. this will stop your background from being drawn

protected override void OnPaintBackground(PaintEventArgs pevent)
{
    //do nothing
}

protected override void OnMove(EventArgs e)
{
    RecreateHandle();
}

// Override the CreateParams property:
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle = 0x00000020; //WS_EX_TRANSPARENT
        return cp;
    }
}

Finally, overriding the OnPaint function you can draw your picturebox.

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;

    //Do your drawing here
}

Using this you could create a custom picturebox with transparency, although note you will get flicker and blurring if you move it around the screen in real-time.

Using this and similar techniques we managed to get a WinForms app, Premex XPort to render with a similar branding to their website. This involved multiple transparent controls, painting hacks and all sorts to get it to display correctly.

In conclusion, the reason why Winforms does this poorly is in Win32 based technologies one control owns one pixel on the screen. There is no way to truly composite pixels with transparency as you would expect in HTML or WPF. Later Windows technologies (WPF) do this particularly well so if you really wish to make heavy use of transparency in your app I would suggest moving to this platform, at least in part (WPF can be hosted within WinForms and vice versa).

Best regards,

躲猫猫 2024-12-30 17:36:11

Win32 控件并不是真正透明的。

您的选择是:

  • 更改为 WPF
  • 使用 PictureBox.Region 剪辑控件不需要的(“透明”)区域
  • 查看是否有第 3 方控件可以执行您想要的操作

Win32 controls are not truly transparent.

Your choices are:

  • Change to WPF
  • Use PictureBox.Region to clip the control's unwanted ("transparent") areas
  • See if there are 3rd party controls which'll do what you want
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文