ImageExtensions Module
Life With Nate
Nate's Poetry Page
Resume (PDF)
Nate's Code
ASP.NET 2.0/3.5
ButtonExtensions
CheckBoxValidator
ConditionalRequiredTextValidator
CSSImageMap
CustomStyle
DateTimePicker
DateTimePickerTemplated
LengthValidator
RestrictInputTextBox
RestrictInputValidator
Rollovers
ControlExtensions
ImageExtensions
RotatedTextHandler
RotatedTextImage
Rotators
SelectedCountValidator
SlideIntoView
StatesDropDownList
UniqueValueValidatorBase
VB.NET 2.0/3.5
Just For Fun
Windows Phone 7
Description
The ImageExtensions Module contains several Extension Methods for the Image, ImageButton, and HtmlImage controls to simply several common tasks.
Example
Image
Me.imgImage1.SetWidthHeight()
Me.imgImage2.AddRollover("~/images/rotatefrog.gif")
Me.imgImage3.AddRollover("~/images/rotatefrog.gif", "~/images/frog.gif")
ImageButton
Me.imgButton1.SetWidthHeight()
Me.imgButton2.AddRollover("~/images/rotatefrog.gif")
Me.imgButton3.AddRollover("~/images/rotatefrog.gif", "~/images/frog.gif")
HtmlImage
Me.imgHtml1.SetWidthHeight()
Me.imgHtml2.AddRollover("~/images/rotatefrog.gif")
Me.imgHtml3.AddRollover("~/images/rotatefrog.gif", "~/images/frog.gif")
Properties & Methods
SetWidthHeight()
This Extension Method extends the Image, ImageButton, and HtmlImage controls by setting the Width and Height properties of the Image or ImageButton or the width and height CSS properties of the HtmlImage. If the ImageUrl or Src property is not set or the image specified does not exist, no action is taken.
AddRollover(ByVal rollover As String)
This Extension Method can be used with the Image, ImageButton, or HtmlImage controls to add a rollover using the control's current ImageUrl or Src property and the specified image. Note: The rollover will not work with the ImageButton if the Enabled property is set to False.
rollover
- The URL of the rollover image
AddRollover(ByVal rollover As String, ByVal initial As String)
This Extension Method can be used with the Image, ImageButton, or HtmlImage controls to set the control's ImageUrl or Src property and add a rollover using the specified images. Note: The rollover will not work with the ImageButton if the Enabled property is set to False.
rollover
- The URL of the rollover image
initial
- The URL of the image to set the ImageUrl or Src property to
Source Code
ImageExtensions.vb:
Imports System.Runtime.CompilerServices Namespace NathanSokalski Public Module ImageExtensions <Extension()> Public Sub SetWidthHeight(ByVal img As System.Web.UI.WebControls.Image) If Not String.IsNullOrEmpty(img.ImageUrl) AndAlso System.IO.File.Exists(img.Page.Server.MapPath(img.ImageUrl)) Then Dim bmp As New System.Drawing.Bitmap(img.Page.Server.MapPath(img.ImageUrl)) img.Width = New Unit(bmp.Width) img.Height = New Unit(bmp.Height) End If End Sub <Extension()> Public Sub SetWidthHeight(ByVal img As System.Web.UI.HtmlControls.HtmlImage) If Not String.IsNullOrEmpty(img.Src) AndAlso System.IO.File.Exists(img.Page.Server.MapPath(img.Src)) Then Dim bmp As New System.Drawing.Bitmap(img.Page.Server.MapPath(img.Src)) img.Style.Add(System.Web.UI.HtmlTextWriterStyle.Width, String.Concat(bmp.Width, "px")) img.Style.Add(System.Web.UI.HtmlTextWriterStyle.Height, String.Concat(bmp.Height, "px")) End If End Sub <Extension()> Public Sub SetWidthHeight(ByVal img As System.Web.UI.WebControls.ImageButton) If Not String.IsNullOrEmpty(img.ImageUrl) AndAlso System.IO.File.Exists(img.Page.Server.MapPath(img.ImageUrl)) Then Dim bmp As New System.Drawing.Bitmap(img.Page.Server.MapPath(img.ImageUrl)) img.Width = New Unit(bmp.Width) img.Height = New Unit(bmp.Height) End If End Sub <Extension()> Public Sub AddRollover(ByVal img As System.Web.UI.WebControls.Image, ByVal rollover As String) img.AddRollover(rollover, img.ImageUrl) End Sub <Extension()> Public Sub AddRollover(ByVal img As System.Web.UI.WebControls.Image, ByVal rollover As String, ByVal initial As String) img.ImageUrl = initial img.Attributes.Add("onmouseover", String.Format("this.src='{0}';", img.Page.ResolveUrl(rollover))) img.Attributes.Add("onmouseout", String.Format("this.src='{0}';", img.Page.ResolveUrl(img.ImageUrl))) End Sub <Extension()> Public Sub AddRollover(ByVal img As System.Web.UI.HtmlControls.HtmlImage, ByVal rollover As String) img.AddRollover(rollover, img.Src) End Sub <Extension()> Public Sub AddRollover(ByVal img As System.Web.UI.HtmlControls.HtmlImage, ByVal rollover As String, ByVal initial As String) img.Src = initial img.Attributes.Add("onmouseover", String.Format("this.src='{0}';", img.Page.ResolveUrl(rollover))) img.Attributes.Add("onmouseout", String.Format("this.src='{0}';", img.Page.ResolveUrl(img.Src))) End Sub <Extension()> Public Sub AddRollover(ByVal img As System.Web.UI.WebControls.ImageButton, ByVal rollover As String) img.AddRollover(rollover, img.ImageUrl) End Sub <Extension()> Public Sub AddRollover(ByVal img As System.Web.UI.WebControls.ImageButton, ByVal rollover As String, ByVal initial As String) img.ImageUrl = initial img.Attributes.Add("onmouseover", String.Format("this.src='{0}';", img.Page.ResolveUrl(rollover))) img.Attributes.Add("onmouseout", String.Format("this.src='{0}';", img.Page.ResolveUrl(img.ImageUrl))) End Sub End Module End Namespace
Remarks
When using the AddRollover Extension Methods with an ImageButton, be sure to keep in mind that the rollover will not work when Enabled="False".