使用 C# WinForms 实现透明图像
-
23-08-2019 - |
题
我正在 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的有2层的图像的:的BackgroundImage和图像,就可以独立地使用彼此的包括拉伸和结算
放在一个PictureBox
大/底部图像,然后添加处理程序到OnPaint
事件并使用重载e.Graphics.DrawImage()
之一。可以使用Image.FromFile()
加载图像。
在小/顶部图像将必须有一个alpha通道和在后台用于叠加工作透明。您应该能够确保这一点很容易在Photoshop或类似的东西。确保在支持alpha通道的格式,比如PNG保存。
在vb.net代码(全学分莱昂西山):
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 已经很好地解决了这个问题)
- 创建Winform
- 创建 x2 图片框
- foreground_pictureBox // “背景”“前面”的图片框
- background_pictureBox // “前景”“后面”的图片框
- 为每个图片框添加“paint”事件
- 在“设计器”中选择对象
- 选择“属性”选项卡(或右键单击并从弹出菜单中选择)
- 选择事件按钮(小闪电)
- 双击“paint”事件右侧的空白字段
- 将以下代码添加到主窗体的“加载”函数中(如果尚未添加,请使用步骤 3 中的方法并选择“加载时”而不是“绘制”)
=
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”调用中,添加您想要在前台显示的任何图形调用。
这个主题似乎在几篇文章中重复出现:
我总是发现,我已经有复合图像自己,使用单一的PictureBox或控制。有两个pictureboxes与透明部分从来没有为我工作。
不隶属于 StackOverflow