在后面的代码中设置图像按钮的不透明度

时间:2013-06-24 10:53:44

标签: c# asp.net opacity

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

<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 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分:2)

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

img.Attributes.Add("class", "myClass");

答案 2 :(得分: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;
    }

并使用返回值进行显示。

here

复制

答案 3 :(得分:1)

在其他条件下的代码中你可以写

butUnlockUser.cssclass="opacity";

.opacity
{
    cursor:none !important ;
    -moz-opacity: 0.40;
    opacity:.40;
    filter: alpha(opacity=40);      
}