1

我有一个现有的图像按钮,想知道是否可以从后面的代码更改此按钮(图像)的不透明度?

<input type="image" runat="server" src="Images/UnlockUser.png" title="Unlock User" id="butUnlockUser" onclick="UnlockUser()"/>

我在页面加载时获得了用户的锁定状态,并希望相应地禁用该按钮,并让它看起来有点褪色。

bool IsLocked = repUser.IsLockedOut(txtDetailUserName.Value);
            if (IsLocked)
            {
                butUnlockUser.Disabled = false;

            }
            else
            {
                butUnlockUser.Disabled = true;
            }

亲切的问候

4

4 回答 4

2

我不确定,但你可以在这种情况下使用不同的 css-class。
在两个类中使用不同的不透明度,并根据您的情况进行更改。

例子

.class1
{
   opacity:0.4; 
   filter:alpha(opacity=40);
}
.class2
{
     opacity:1; 
     filter:alpha(opacity=100);
}

并在条件下使用它

bool IsLocked = repUser.IsLockedOut(txtDetailUserName.Value);
if (IsLocked)
{
     butUnlockUser.Disabled = false;
     butUnlockUser.CssClass ="class1";
}
else
{
     butUnlockUser.Disabled = true;
     butUnlockUser.CssClass ="class2";
}
于 2013-06-24T11:02:58.703 回答
2

您可以创建一个 css 类来设置不透明度,然后在后面的代码中将该类添加到图像中,如下所示:

img.Attributes.Add("class", "myClass");
于 2013-06-24T11:09:05.017 回答
1

您可以使用这种替代方法:

 public static Bitmap ChangeOpacity(Image img, float opacityvalue)
    {
        Bitmap bmp = new Bitmap(img.Width,img.Height); // Determining Width and Height of Source Image
        Graphics graphics = Graphics.FromImage(bmp);
        ColorMatrix colormatrix = new ColorMatrix();
        colormatrix.Matrix33 = opacityvalue;
        ImageAttributes imgAttribute = new ImageAttributes();
        imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
        graphics.Dispose();   // Releasing all resource used by graphics 
        return bmp;
    }

并使用返回值进行显示。

从这里复制

于 2013-06-24T10:58:48.150 回答
1

在 else 条件下的代码后面你可以写

butUnlockUser.cssclass="opacity";

.opacity
{
    cursor:none !important ;
    -moz-opacity: 0.40;
    opacity:.40;
    filter: alpha(opacity=40);      
}
于 2013-06-24T11:10:07.033 回答