使用C#WinForms的透明图像

时间:2008-12-27 18:30:58

标签: c# .net image transparency picturebox

我正在使用VS 2008中的Windows窗体应用程序,我希望在另一个图像的顶部显示一个图像,顶部图像是gif或具有透明部分的图像。

基本上我有一个很大的图像,我想在它上面放一个小图像,这样它们就像是一个图像给用户。

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

6 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分:6)

PictureBox有2层图像:BackgroundImage和Image,您可以彼此独立使用,包括绘图和清除。

答案 2 :(得分:4)

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

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

答案 3 :(得分: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

答案 4 :(得分:1)

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

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

  1. 创建Winform
  2. 创建x2 pictureBoxes
    • foreground_pictureBox //'背景'前面的图片框
    • background_pictureBox //图片框'在'前景'后面
  3. 为每个pictureBox添加'paint'事件
    • 在'designer'中选择对象
    • 选择“属性”标签(或右键单击并从弹出菜单中选择)
    • 选择事件按钮(小闪电)
    • 双击“paint”事件右侧的空白字段
  4. 将以下代码添加到主窗体的'load'函数中(如果尚未添加,请使用步骤3中的方法并选择'on load'而不是'paint')
  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。在'paint'调用'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

答案 5 :(得分:0)

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