带有C#WinForms的透明图像

问题描述 投票:22回答:7

我正在VS 2008中开发Windows窗体应用程序,并且我想在另一个图像的顶部显示一个图像,顶部图像是gif或具有透明部分的东西。

基本上,我有一个大图像,如果要在上面放一个小图像,以便它们对于用户来说看起来像是一个图像。

我一直在尝试使用图片框,但这似乎没有用,有什么建议吗?

c# .net image transparency picturebox
7个回答
25
投票

几天前我也处于类似情况。您可以创建一个透明控件来托管图像。

using System;
using System.Windows.Forms;
using System.Drawing;

public class TransparentControl : Control
{
    private readonly Timer refresher;
    private Image _image;

    public TransparentControl()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        BackColor = Color.Transparent;
        refresher = new Timer();
        refresher.Tick += TimerOnTick;
        refresher.Interval = 50;
        refresher.Enabled = true;
        refresher.Start();
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }

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


    protected override void OnPaint(PaintEventArgs e)
    {
        if (_image != null)
        {
            e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
       //Do not paint background
    }

    //Hack
    public void Redraw()
    {
        RecreateHandle();
    }

    private void TimerOnTick(object source, EventArgs e)
    {
        RecreateHandle();
        refresher.Stop();
    }

    public Image Image
    {
        get
        {
            return _image;
        }
        set
        {
            _image = value;
            RecreateHandle();
        }
    }
}

6
投票

PictureBox具有两层图像:BackgroundImage和Image,您可以相互独立使用,包括绘制和清除。


4
投票

将大/底图像放置在PictureBox上,然后向OnPaint事件添加处理程序,并使用e.Graphics.DrawImage()重载之一。您可以使用Image.FromFile()加载图像。

小/顶部图像必须具有Alpha通道,并且在背景中是透明的,叠加层才能正常工作。您应该能够在Photoshop或类似工具中很容易地确保这一点。确保以支持Alpha通道的格式保存,例如PNG。


3
投票

vb.net代码(全部归功于Leon Tayson):

Imports System
Imports System.Windows.Forms
Imports System.Drawing

Public Class TransparentControl
    Inherits Control

    Private ReadOnly Local_Timer As Timer
    Private Local_Image As Image

    Public Sub New()
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        BackColor = Color.Transparent
        Local_Timer = New Timer
        With Local_Timer
            .Interval = 50
            .Enabled = True
            .Start()
        End With

        AddHandler Local_Timer.Tick, AddressOf TimerOnClick

    End Sub

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams
            cp = MyBase.CreateParams
            cp.ExStyle = &H20
            Return cp
        End Get
    End Property

    Protected Overrides Sub OnMove(ByVal e As System.EventArgs)
        MyBase.OnMove(e)
        RecreateHandle()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        If Local_Image IsNot Nothing Then _
            e.Graphics.DrawImage(Local_Image, New Rectangle(0, 0, (Width / 2) - (Local_Image.Width / 2), (Height / 2) - (Local_Image.Height / 2)))

    End Sub

    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        ' DO NOT PAINT BACKGROUND
    End Sub

    ''' <summary>
    ''' Hack
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ReDraw()
        RecreateHandle()
    End Sub

    Private Sub TimerOnClick(ByVal sender As Object, ByVal e As System.EventArgs)
        RecreateHandle()
        Local_Timer.Stop()

    End Sub

    Public Property Image As Image
        Get
            Return Local_Image
        End Get
        Set(ByVal value As Image)
            Local_Image = value
            RecreateHandle()
        End Set
    End Property
End Class

1
投票

此回复的底部引用了类似的帖子列表。

此回复解决了pictureBoxes和Winforms(在下面的其他帖子中,有几位重申WPF已经很好地解决了这个问题)

  1. 创建Winform
  2. 创建x2图片框
    • foreground_pictureBox //图片框位于“背景”的“前面”
    • background_pictureBox //图片框位于“前景”之后”>
  3. 为每个pictureBox添加'paint'事件
    • 在“设计器”中选择对象
    • 选择“属性”选项卡(或右键单击并从弹出菜单中选择)
    • 选择事件按钮(小闪电)
    • 双击'paint'事件右侧的空白区域
  4. 将以下代码添加到主窗体的“加载”功能(如果尚未添加,请使用步骤3中的方法,然后选择“加载时”而不是“绘画”)
  5. =

private void cFeedback_Form_Load(object sender, EventArgs e)
{
    ...
    // Ensure that it is setup with transparent background
    foreground_pictureBox.BackColor = Color.Transparent;

    // Assign it's 'background'
    foreground_pictureBox.Parent = background_pictureBox;
    ...
}

5。在“画图”中调用“ background_pictureBox”:

=

private void background_pictureBox_Paint(object sender, PaintEventArgs e)
{
    ...foreground_pictureBox_Paint(sender, e);
}

6。在“ foreground_pictureBox_Paint”调用中,添加要在前景中显示的任何图形调用。

此主题似乎在几篇文章中重复出现:

how-to-make-picturebox-transparent

c-sharp-picturebox-transparent-background-doesnt-seem-to-work

make-overlapping-picturebox-transparent-in-c-net

a-picturebox-problem


0
投票

我总是发现我必须使用单个图片框或控件自己合成图像。有两个带有透明零件的画框对我来说从来没有用过。


0
投票

我在.Net Framework 4.5上找到了Visual Studio 2019的解决方案。我将更新帖子。我今天很忙。

© www.soinside.com 2019 - 2024. All rights reserved.