By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

maintaining image aspect ratio in datalist

P: n/a
Hello All,

Does anyone know how to dynamically change the width and height properties
of an image in a datalist in order to maintain the original aspect ratio?

I've managed to do this with a single image (see below) but don't have a
clue how to apply the same approach in a datalist, perhaps with a custom
databinding expression (or what?).

I would also prefer not displaying the image if it's blank so being able to
set visible false when the file does not exist would be useful too.

Thanks for any suggestions.


code for adjusting size and maintaining aspect ratio mostly courtesy of
Scott Mitchell at
(thanks Scott!)

If File.Exists(strImagePath) Then

Dim myImage As System.Drawing.Image =

Dim imgHeight As Integer = myImage.Height

Dim imgWidth As Integer = myImage.Width

Dim intMaxWidth As Integer = 200

Dim intMaxHeight As Integer = 200

Dim ImageScaleFactor As Double = PSDB.ImageScaleFactor(myImage.Height,
myImage.Width, intMaxHeight , intMaxWidth )

Me.imgThumbnail.Height = System.Web.UI.WebControls.Unit.Pixel(myImage.Heigh t
* ImageScaleFactor)

Me.imgThumbnail.Width = System.Web.UI.WebControls.Unit.Pixel(myImage.Width *

Me.imgThumbnail.ImageUrl = strImagePath

Else 'file does not exist so hide the image box

Me.imgThumbnail.Visible = False

End If

Public Shared Function ImageScaleFactor(ByVal ImgHeight As Integer, ByVal
ImgWidth As Integer, ByVal MaxHeight As Integer, ByVal MaxWidth As Integer)
As Double

If ImgWidth MaxWidth Or ImgHeight MaxHeight Then

'Determine which dimension is off by more

Dim DeltaWidth As Integer = ImgWidth - MaxWidth

Dim DeltaHeight As Integer = ImgHeight - MaxHeight

Dim ScaleFactor As Double

If deltaHeight deltaWidth Then

'Scale by the height

scaleFactor = MaxHeight / ImgHeight


'Scale by the Width

scaleFactor = MaxWidth / ImgWidth

End If

ImageScaleFactor = ScaleFactor

End If

End Function
Sep 28 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.