简体   繁体   中英

setting the opacity of image button in code behind

I have an existing image button and was wondering if it is possible to change the opacity of this button(the image) from code behind?.

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

I am getting the locked status of the user on page load and want to disable the button accordingly and also give it a bit of a faded look.

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

            }
            else
            {
                butUnlockUser.Disabled = true;
            }

kind regards

I am not sure but you can use different css-class in this condition.
Use different opacity in both the classes and change it according to your condition.

example

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

and use it on condition

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

You can use this alternative method:

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

and use the return value for showing.

copied from here

in the code behind under else condition you can write

butUnlockUser.cssclass="opacity";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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