asp.net - How to change an Image width programmatically

Set or change Image width programmatically
Image is an asp.net web server control which allow us to work with images in server side code. Image server control display an image on web page. we can set or change image width programmatically at run time by using Image server control's built in property.

Image server control's Width property get or set the width of the Image control. this property value type is System.Web.UI.WebControls.Unit. the 'Unit' represents the width of the Image control. we can simply set a numeric value for this Width property value.

the following asp.net c# example code demonstrate us how can we set or change Image server control's width programmatically at run time in an asp.net application.
ImageWidth.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Image1.Width = 300;
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        Image1.Width = 250;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to set, change Image width programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">Image Example: Change Width</h2>
        <asp:Image 
             ID="Image1" 
             runat="server"
             ImageUrl="~/Images/Birds.jpg"
             BorderColor="SkyBlue"
             BorderWidth="3"
             BorderStyle="Inset"
             />
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="Crimson"
             Text="Set Image Width 300"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
        <asp:Button 
             ID="Button2" 
             runat="server"
             ForeColor="Crimson"
             Text="Set Image Width 250"
             Height="45"
             OnClick="Button2_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>








Related asp.net examples