如何使一个图片真正透明透明、图片

2023-09-02 01:51:07 作者:你给的痛几吨重

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

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.

现在的问题是,当我将图片框的应用程序,该图片的背景走动太奇怪 - 仿佛在PictureBox具有背景本身

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.

之前:

之后(在移动):

有些code:

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

我也试着做了PictureBox的双缓冲,但这并不能工作。

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

推荐答案

在的WinForms完全不适合透明度用户控件,它是可能的。请参见这里这篇文章。在这笔者建议从面板而不是用户控件派生并overridding的OnPaintBackground方法什么都不做。这将停止你的背景被吸入

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;
    }
}

最后,重写的OnPaint功能,你可以画出你的图片框。

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.

使用这个和类似的技术,我们得到了一个WinForms应用程序, preMEX的XPort 以呈现一个类似的品牌推广到他们的网站。这项工作涉及多个透明的管制,绘画黑客和各种不同的人得到它正确显示。

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.

总之,为什么的WinForms做到这一点不好的原因是基于Win32的技术之一拥有控制屏幕上的一个像素。有没有办法真正的复合像素的透明度,如你所期望的HTML或WPF。后来的Windows技术(WPF)做到这一点特别好,所以如果你真的想使大量使用的透明度,您的应用程序,我建议迁移到这个平台上,至少部分地(WPF可以WinForms和反之亦然中被托管)。

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).

最好的问候,

 
精彩推荐
图片推荐