我正在VS 2008中开发Windows窗体应用程序,并且我想在另一个图像的顶部显示一个图像,顶部图像是gif或具有透明部分的东西。
基本上,我有一个大图像,如果要在上面放一个小图像,以便它们对于用户来说看起来像是一个图像。
我一直在尝试使用图片框,但这似乎没有用,有什么建议吗?
几天前我也处于类似情况。您可以创建一个透明控件来托管图像。
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();
}
}
}
PictureBox具有两层图像:BackgroundImage和Image,您可以相互独立使用,包括绘制和清除。
将大/底图像放置在PictureBox
上,然后向OnPaint
事件添加处理程序,并使用e.Graphics.DrawImage()
重载之一。您可以使用Image.FromFile()
加载图像。
小/顶部图像必须具有Alpha通道,并且在背景中是透明的,叠加层才能正常工作。您应该能够在Photoshop或类似工具中很容易地确保这一点。确保以支持Alpha通道的格式保存,例如PNG。
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
此回复的底部引用了类似的帖子列表。
此回复解决了pictureBoxes和Winforms(在下面的其他帖子中,有几位重申WPF已经很好地解决了这个问题)
=
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
我总是发现我必须使用单个图片框或控件自己合成图像。有两个带有透明零件的画框对我来说从来没有用过。
我在.Net Framework 4.5上找到了Visual Studio 2019的解决方案。我将更新帖子。我今天很忙。