Export (0) Print
Expand All

Image Class

Displays an image on a Web page.

For a list of all members of this type, see Image Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.Image
            System.Web.UI.WebControls.ImageButton

[Visual Basic]
Public Class Image
   Inherits WebControl
[C#]
public class Image : WebControl
[C++]
public __gc class Image : public WebControl
[JScript]
public class Image extends WebControl

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Use the Image control to display an image on the Web page. The path to the displayed image is specified by setting the ImageUrl property. You can specify the text to display in place of image when the image is not available by setting the AlternateText property. The alignment of the image in relation to other elements on the Web page is specified by setting ImageAlign property.

Note   This control only displays an image. If you need to capture mouse clicks on the image, use the ImageButton control.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to create an Image control on a Web page.

[Visual Basic] 
<%@ Page Language="VB" %>
<html>
<head>

   <script language="VB" runat="server">

      Sub Button_Click(sender As Object, e As EventArgs)
         Select Case DropList1.SelectedIndex
            Case 0:
               Image1.ImageAlign = ImageAlign.NotSet
         
            Case 1:
               Image1.ImageAlign = ImageAlign.Left

            Case 2:
               Image1.ImageAlign = ImageAlign.Right

            Case 3:
               Image1.ImageAlign = ImageAlign.Baseline

            Case 4:
               Image1.ImageAlign = ImageAlign.Top

            Case 5:
               Image1.ImageAlign = ImageAlign.Middle

            Case 6:
               Image1.ImageAlign = ImageAlign.Bottom
         
            Case 7:
               Image1.ImageAlign = ImageAlign.AbsBottom

            Case 8:
               Image1.ImageAlign = ImageAlign.AbsMiddle

            Case 9:
               Image1.ImageAlign = ImageAlign.TextTop

            Case Else:
               Image1.ImageAlign = ImageAlign.NotSet

         End Select
      End Sub

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Image Example</h3>

      <font size="5">

         Text Text Text Text Text Text Text Text Text Text Text Text 
         Text Text Text Text Text Text Text Text Text Text Text Text 
         Text Text Text Text Text Text Text Text Text Text Text Text

      </font>  
 
      <asp:Image id="Image1" runat="server"
           AlternateText="Image Text"
           ImageAlign="left"
           ImageUrl="images/image1.jpg"/>

      <hr>
    
      Select Image Align: <br>

      <asp:DropDownList id="DropList1"
           runat="server">

         <asp:ListItem>NotSet</asp:ListItem>
         <asp:ListItem>Left</asp:ListItem>
         <asp:ListItem>Right</asp:ListItem>
         <asp:ListItem>BaseLine</asp:ListItem>
         <asp:ListItem>Top</asp:ListItem>
         <asp:ListItem>Middle</asp:ListItem>
         <asp:ListItem>Bottom</asp:ListItem>
         <asp:ListItem>AbsBottom</asp:ListItem>
         <asp:ListItem>AbsMiddle</asp:ListItem>
         <asp:ListItem>TextTop</asp:ListItem>     

      </asp:DropDownList>

      <br><br>

      <asp:Button id="Button1"
           Text="Apply Image Alignment"
           OnClick="Button_Click" 
           runat="server"/>
  
   </form>

</body>
</html>
   

[C#] 
<%@ Page Language="C#" %>
<html>
<head>

   <script language="C#" runat="server">

      void Button_Click(Object sender, EventArgs e)
      {
         switch(DropList1.SelectedIndex)
         {
            case 0:
               Image1.ImageAlign = ImageAlign.NotSet;
               break;
         
            case 1:
               Image1.ImageAlign = ImageAlign.Left;
               break;

            case 2:
               Image1.ImageAlign = ImageAlign.Right;
               break;

            case 3:
               Image1.ImageAlign = ImageAlign.Baseline;
               break;

            case 4:
               Image1.ImageAlign = ImageAlign.Top;
               break;

            case 5:
               Image1.ImageAlign = ImageAlign.Middle;
               break;

            case 6:
               Image1.ImageAlign = ImageAlign.Bottom;
               break;
         
            case 7:
               Image1.ImageAlign = ImageAlign.AbsBottom;
               break;

            case 8:
               Image1.ImageAlign = ImageAlign.AbsMiddle;
               break;

            case 9:
               Image1.ImageAlign = ImageAlign.TextTop;
               break;

            default:
               Image1.ImageAlign = ImageAlign.NotSet;
               break;

         }

      }

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Image Example</h3>

      <font size="5">

         Text Text Text Text Text Text Text Text Text Text Text Text 
         Text Text Text Text Text Text Text Text Text Text Text Text 
         Text Text Text Text Text Text Text Text Text Text Text Text

      </font> 
 
      <asp:Image id="Image1" runat="server"
           AlternateText="Image Text"
           ImageAlign="left"
           ImageUrl="images/image1.jpg"/>

      <hr>
    
      Select Image Align: <br>

      <asp:DropDownList id="DropList1"
           runat="server">

         <asp:ListItem>NotSet</asp:ListItem>
         <asp:ListItem>Left</asp:ListItem>
         <asp:ListItem>Right</asp:ListItem>
         <asp:ListItem>BaseLine</asp:ListItem>
         <asp:ListItem>Top</asp:ListItem>
         <asp:ListItem>Middle</asp:ListItem>
         <asp:ListItem>Bottom</asp:ListItem>
         <asp:ListItem>AbsBottom</asp:ListItem>
         <asp:ListItem>AbsMiddle</asp:ListItem>
         <asp:ListItem>TextTop</asp:ListItem>     

      </asp:DropDownList>

      <br><br>

      <asp:Button id="Button1"
           Text="Apply Image Alignment"
           OnClick="Button_Click" 
           runat="server"/>
  
   </form>

</body>
</html>
   

[JScript] 
<%@ Page Language="JSCRIPT" %>
<html>
<head>

   <script language="JSCRIPT" runat="server">

      function Button_Click(sender : Object, e : EventArgs){
         switch(DropList1.SelectedIndex){
            case 0:
               Image1.ImageAlign = ImageAlign.NotSet
               break
            case 1:
               Image1.ImageAlign = ImageAlign.Left
               break
            case 2:
               Image1.ImageAlign = ImageAlign.Right
               break
            case 3:
               Image1.ImageAlign = ImageAlign.Baseline
               break
            case 4:
               Image1.ImageAlign = ImageAlign.Top
               break
            case 5:
               Image1.ImageAlign = ImageAlign.Middle
               break
            case 6:
               Image1.ImageAlign = ImageAlign.Bottom
               break
            case 7:
               Image1.ImageAlign = ImageAlign.AbsBottom
               break
            case 8:
               Image1.ImageAlign = ImageAlign.AbsMiddle
               break
            case 9:
               Image1.ImageAlign = ImageAlign.TextTop
               break
            default:
               Image1.ImageAlign = ImageAlign.NotSet
               break
         }
      }

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Image Example</h3>

      <font size="5">

         Text Text Text Text Text Text Text Text Text Text Text Text 
         Text Text Text Text Text Text Text Text Text Text Text Text 
         Text Text Text Text Text Text Text Text Text Text Text Text

      </font> 
 
      <asp:Image id="Image1" runat="server"
           AlternateText="Image Text"
           ImageAlign="left"
           ImageUrl="images/image1.jpg"/>

      <hr>
    
      Select Image Align: <br>

      <asp:DropDownList id="DropList1"
           runat="server">

         <asp:ListItem>NotSet</asp:ListItem>
         <asp:ListItem>Left</asp:ListItem>
         <asp:ListItem>Right</asp:ListItem>
         <asp:ListItem>BaseLine</asp:ListItem>
         <asp:ListItem>Top</asp:ListItem>
         <asp:ListItem>Middle</asp:ListItem>
         <asp:ListItem>Bottom</asp:ListItem>
         <asp:ListItem>AbsBottom</asp:ListItem>
         <asp:ListItem>AbsMiddle</asp:ListItem>
         <asp:ListItem>TextTop</asp:ListItem>     

      </asp:DropDownList>

      <br><br>

      <asp:Button id="Button1"
           Text="Apply Image Alignment"
           OnClick="Button_Click" 
           runat="server"/>
  
   </form>

</body>
</html>
   

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

Image Members | System.Web.UI.WebControls Namespace | ImageUrl | AlternateText | ImageAlign

Show:
© 2014 Microsoft