如何使用作按钮的禁用图片框变灰? [英] How can I gray-out a disabled PictureBox used as Button?

查看:24
本文介绍了如何使用作按钮的禁用图片框变灰?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图片框控件用作应用主仪表板中的按钮.
PictureBox 当然有一个 Image 来标识 Button 功能.

如果我使用普通的按钮控件,当禁用时,按钮的图像会自动变灰.
使用 PictureBox 不会发生这种情况.

如何使用图片框生成相同的效果.

解决方案

Option 1: CustomControl (PictureBox) + ColorMatrix

由于您不想使用按钮,当控件被禁用时它会使您的图像变灰,您可以使用

使用 System.ComponentModel;使用 System.Drawing;使用 System.Windows.Forms;[设计师类别(代码")]公共类 ButtonPicture : PictureBox{私有图像 m_sourceImage = null;私有图像 m_grayImage = null;公共按钮图片(){}受保护的覆盖无效 OnEnabledChanged(EventArgs e) {base.OnEnabledChanged(e);this.BackgroundImage = this.Enabled ?m_sourceImage : m_grayImage;}受保护的覆盖无效 OnBackgroundImageChanged(EventArgs e) {base.OnBackgroundImageChanged(e);如果 (this.BackgroundImage == m_sourceImage ||this.BackgroundImage == m_grayImage) 返回;m_sourceImage = this.BackgroundImage;m_grayImage = m_sourceImage.ToGrayScale();}受保护的覆盖无效处置(布尔处置){如果(处置){m_grayImage.Dispose();}base.Dispose(处置);}}

扩展方法:

使用 System.Drawing;使用 System.Drawing.Imaging;使用 System.IO;公共静态类 ImageExtensions{静态 ColorMatrix grayMatrix = new ColorMatrix(new float[][]{新浮点[] { .2126f, .2126f, .2126f, 0, 0 },新浮点[] { .7152f, .7152f, .7152f, 0, 0 },新浮点[] { .0722f, .0722f, .0722f, 0, 0 },新浮点[] { 0, 0, 0, 1, 0 },新浮点[] { 0, 0, 0, 0, 1 }});公共静态位图 ToGrayScale(此图像源){var grayImage = new Bitmap(source.Width, source.Height, source.PixelFormat);grayImage.SetResolution(source.Horizo​​ntalResolution, source.VerticalResolution);使用 (var g = Graphics.FromImage(grayImage))使用 (var 属性 = 新 ImageAttributes()) {attributes.SetColorMatrix(grayMatrix);g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),0, 0, source.Width, source.Height, GraphicsUnit.Pixel, 属性);返回灰色图像;}}}



选项 2:ControlPaint + PictureBox.Paint 事件(蹩脚)

您可以使用

private void buttonPicture_Paint(object sender, PaintEventArgs e){var pict = 发件人为图片框;if (pict != null && (!pict.Enabled)) {使用 (var img = new Bitmap(pict.Image, pict.ClientSize)) {ControlPaint.DrawImageDisabled(e.Graphics, img, 0, 0, pict.BackColor);}}}

I'm using Picturebox control as a Button in my app's main dashboard.
The PictureBox has of course an Image that identifies the Button function.

If I use normal Button controls, when disabled, the Buttons' Image it grayed out automatically.
This doesn't happen using a PictureBox.

How can I generated the same effect using a Picturebox.

解决方案

Option 1: CustomControl (PictureBox) + ColorMatrix

Since you don't want to use a Button, which would gray-out the Image for you when the Control is disabled, you can use a ColorMatrix to change the PictureBox.BackgroundImage or (Image) to a gray scale.

The GrayScale matrix you see here uses well-known values to convert an Image to a scaled gray color representation. You can find other interpretations of the same matrix object that may generate different results.
It can be fun to test some or tweak it yourself.

The GrayScale procedure is implemented as an Extension method, since it may come in handy in other situations.
It extends the Image class, adding a ToGrayScale() method (you can of course also extend the Bitmap class: you just need to call the Image extension, casting Bitmap to Image, or the other way around, as you prefer).


Assuming you have a Custom Control, when the BackgroundImage is changed, you create a GrayScale representation of it and store it.

Then override OnEnabledChanged to change the BackgroundImage to either the original or its GrayScale version. A simple check prevents the code from generating a new GrayScale image when the BackgroundImage is changed internally (it's a somewhat simplified method, you should paint the background instead. I'll update it when I can).

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);
    }
}

The extension method:

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;
        }
    }
}



Option 2: ControlPaint + PictureBox.Paint event (lame)

You can use the ControlPaint.DrawImageDisabled() method to draw a disabled Bitmap.

  • The Graphics object the methods needs is provided by the Control's PaintEventArgs: subscribe to the PictureBox.Paint event and pass the e.Graphics object to the method.
  • The Image argument can be a copy of the PictureBox.Image property value.
  • The only advantage is that you need quite less code, but the default implementation cannot be controlled and the result is, well, questionable (but probably good enough for a generic use).

► Compare the Image on the left, disabled using a ColorMatrix, with the Image on the right, disabled using the ControlPaint method:

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);
        }
    }
}

这篇关于如何使用作按钮的禁用图片框变灰?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆