繁体   English   中英

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

[英]setting the opacity of image button in code behind

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

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

亲切的问候

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

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

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

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

您可以使用以下替代方法:

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

并使用返回值进行显示。

这里复制

在其他条件下的代码中,您可以编写

butUnlockUser.cssclass="opacity";

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM