【发布时间】:2014-04-22 13:29:42
【问题描述】:
我想制作一个应用了线性不透明度的位图。 (即,它在左侧更加不透明,并且随着靠近右侧逐渐变得不透明。)
这可能吗?我知道它可能具有恒定的不透明度级别。
【问题讨论】:
标签: c# .net winforms controls opacity
我想制作一个应用了线性不透明度的位图。 (即,它在左侧更加不透明,并且随着靠近右侧逐渐变得不透明。)
这可能吗?我知道它可能具有恒定的不透明度级别。
【问题讨论】:
标签: c# .net winforms controls opacity
将位图放入PictureBox 控件中。然后您将不得不使用PictureBox 的Paint 事件处理程序来进行淡入淡出。像
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(pictureBox.Image, 0, 0,
pictureBox.ClientRectangle, GraphicsUnit.Pixel);
Color left = Color.FromArgb(128, Color.Blue);
Color right = Color.FromArgb(128, Color.Red);
LinearGradientMode direction = LinearGradientMode.Horizontal;
LinearGradientBrush brush = new LinearGradientBrush(
pictureBox.ClientRectangle, left, right, direction);
e.Graphics.FillRectangle(brush, pictureBox.ClientRectangle);
}
此示例将图像叠加层从蓝色渐变为红色。我相信您可以使用它来获得您想要的工作。
我希望这会有所帮助。
【讨论】:
我知道它可以有一个恒定的不透明度水平
所以不要让它保持不变,LinearGradientBrush 可以毫无问题地插入 alpha 值。设置了 BackgroundImage 的表单的简单演示:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
var rc = new Rectangle(20, 20, this.ClientSize.Width - 40, 50);
using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
rc,
Color.FromArgb(255, Color.BlueViolet),
Color.FromArgb(0, Color.BlueViolet),
0f)) {
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
e.Graphics.FillRectangle(brush, rc);
}
}
制作:
【讨论】: