Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - maintaining image aspect ratio in datalist

 
Thread Tools Search this Thread
Old 09-28-2006, 06:19 PM   #1
Default maintaining image aspect ratio in datalist


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.

Mike

code for adjusting size and maintaining aspect ratio mostly courtesy of
Scott Mitchell at http://aspnet.4guysfromrolla.com/art...11503-1.2.aspx
(thanks Scott!)

If File.Exists(strImagePath) Then

Dim myImage As System.Drawing.Image =
System.Drawing.Image.FromFile(strImagePath)

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 *
ImageScaleFactor)

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

Else

'Scale by the Width

scaleFactor = MaxWidth / ImgWidth

End If

ImageScaleFactor = ScaleFactor

End If

End Function




mharness
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dealing w/ a "Stretched" fullscreen image file... aspect ratio conversion? kemurphy@southwesteng.com DVD Video 27 06-05-2005 05:24 PM
What program will convert a MPEG2 video to MPEG4 retaining a playback aspect ratio? Dima DVD Video 55 02-21-2005 02:24 AM
What Windows program will convert MPEG2 video to MPEG4 retaining its playback aspect ratio and its video resolution? Dima DVD Video 0 01-24-2005 07:14 AM
Yet another aspect ratio question Elias DVD Video 21 06-22-2004 09:31 PM
Yet another aspect ratio question Elias DVD Video 1 06-17-2004 12:58 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46