You*_*bal 1 c# graphics picturebox winforms
我在我的应用程序的主仪表板中使用 Picturebox 控件作为按钮。
PictureBox 当然有一个 Image 来标识 Button 函数。
如果我使用普通按钮控件,禁用时,按钮图像会自动变灰。
使用 PictureBox 不会发生这种情况。
如何使用图片框生成相同的效果。
选项 1:自定义控件 (PictureBox) + ColorMatrix
由于您不想使用按钮,它会在禁用控件时为您显示灰色图像,因此您可以使用ColorMatrix将PictureBox.BackgroundImage
或(图像)更改为灰度。
您在此处看到的 GrayScale 矩阵使用众所周知的值将图像转换为缩放的灰色表示。您可以找到可能产生不同结果的同一矩阵对象的其他解释。
测试一些或自己调整它会很有趣。
GrayScale 过程是作为扩展方法实现的,因为它在其他情况下可能会派上用场。
它扩展了 Image 类,添加了一个ToGrayScale()
方法(您当然也可以扩展 Bitmap 类:您只需要调用 Image 扩展,将 Bitmap 转换为 Image,或者反过来,根据您的喜好)。
假设您有一个自定义控件,当更改背景图像时,您将创建它的灰度表示并存储它。
然后覆盖OnEnabledChanged
以将 BackgroundImage 更改为原始版本或其 GrayScale 版本。一个简单的检查可以防止代码在内部更改 BackgroundImage 时生成新的 GrayScale 图像(这是一种稍微简化的方法,您应该改为绘制背景。我会尽可能更新它)。
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
[DesignerCategory("Code")]
public class ButtonPicture : PictureBox
{
private Image m_sourceImage = null;
private Image m_grayImage = null;
public ButtonPicture() { }
protected override void OnEnabledChanged(EventArgs e) {
base.OnEnabledChanged(e);
this.BackgroundImage = this.Enabled ? m_sourceImage : m_grayImage;
}
protected override void OnBackgroundImageChanged(EventArgs e) {
base.OnBackgroundImageChanged(e);
if (this.BackgroundImage == m_sourceImage ||
this.BackgroundImage == m_grayImage) return;
m_sourceImage = this.BackgroundImage;
m_grayImage = m_sourceImage.ToGrayScale();
}
protected override void Dispose(bool disposing) {
if (disposing) {
m_grayImage.Dispose();
}
base.Dispose(disposing);
}
}
Run Code Online (Sandbox Code Playgroud)
扩展方法:
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public static class ImageExtensions
{
static ColorMatrix grayMatrix = new ColorMatrix(new float[][]
{
new float[] { .2126f, .2126f, .2126f, 0, 0 },
new float[] { .7152f, .7152f, .7152f, 0, 0 },
new float[] { .0722f, .0722f, .0722f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
});
public static Bitmap ToGrayScale(this Image source) {
var grayImage = new Bitmap(source.Width, source.Height, source.PixelFormat);
grayImage.SetResolution(source.HorizontalResolution, source.VerticalResolution);
using (var g = Graphics.FromImage(grayImage))
using (var attributes = new ImageAttributes()) {
attributes.SetColorMatrix(grayMatrix);
g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),
0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);
return grayImage;
}
}
}
Run Code Online (Sandbox Code Playgroud)
选项 2:ControlPaint + PictureBox.Paint 事件(跛脚)
您可以使用ControlPaint.DrawImageDisabled()方法绘制禁用的 位图。
PictureBox.Paint
事件并将e.Graphics
对象传递给方法。Image
参数可以是复印件PictureBox.Image
属性值。? 比较左侧的 Image,使用 ColorMatrix禁用,右侧的 Image,使用 ControlPaint 方法禁用:
private void buttonPicture_Paint(object sender, PaintEventArgs e)
{
var pict = sender as PictureBox;
if (pict != null && (!pict.Enabled)) {
using (var img = new Bitmap(pict.Image, pict.ClientSize)) {
ControlPaint.DrawImageDisabled(e.Graphics, img, 0, 0, pict.BackColor);
}
}
}
Run Code Online (Sandbox Code Playgroud)