Hi i created a method that will scale pictures that users upload to be viewed in an ImageWell. However, i’m getting results that cause the image to not scale properly. Does anyone see anything i am doing wrong?
Code:
[code] dim landscape as Boolean
if CurrentPicture.Picture.Width > CurrentPicture.Picture.Height then
landscape = true
elseif CurrentPicture.Picture.Height > CurrentPicture.Picture.Width then
landscape = false
end if
dim scaleDown as Boolean
if CurrentPicture.Picture.Width > CurrentPicture.Width then
scaleDown = true
elseif CurrentPicture.Picture.Height > CurrentPicture.Height then
scaleDown = true
else
scaleDown = false
end if
dim ratio as double
if landscape then
if scaleDown then
ratio = CurrentPicture.Picture.Width / CurrentPicture.Width
ScaleWidth = CurrentPicture.Picture.Width / ratio
ScaleHeight = CurrentPicture.Picture.Height / ratio
else
ratio = CurrentPicture.Width / CurrentPicture.Picture.Width
ScaleWidth = CurrentPicture.Picture.Width * ratio
ScaleHeight = CurrentPicture.Picture.Height * ratio
end if
else
if scaleDown then
ratio = CurrentPicture.Picture.Height / CurrentPicture.Height
ScaleWidth = CurrentPicture.Picture.Width / ratio
ScaleHeight = CurrentPicture.Picture.Height / ratio
else
ratio = CurrentPicture.Height / CurrentPicture.Picture.Height
ScaleWidth = CurrentPicture.Picture.Width * ratio
ScaleHeight = CurrentPicture.Picture.Height * ratio
end if
end if[/code]
Results: Some Images display as expected.
link text
link text
link text
link text
This is how I do it (web version):
ResizePictureToImageView(SourceImage as picture, byref DestHeight as integer, byref DestWidth as integer, byref xPos as integer, byref yPos as integer, FrameHeight as integer, FrameWidth as integer) as picture
if SourceImage.Height > SourceImage.Width then //Portrait
if SourceImage.Height > FrameHeight then
DestHeight = FrameHeight
DestWidth = DestHeight * (SourceImage.Width / SourceImage.Height)
xPos = 0 + (FrameWidth - DestWidth) / 2
yPos = 0
else
DestHeight = SourceImage.Height
DestWidth = SourceImage.Width
xPos = 0 + (FrameWidth - DestWidth) / 2
yPos = 0 + (FrameHeight - DestHeight) / 2
end if
else // Landscape
if SourceImage.Width > FrameWidth then
DestWidth = FrameWidth
'DestHeight = DestWidth * (SourceImage.Height / SourceImage.Width)
DestHeight = SourceImage.Height * (FrameWidth / SourceImage.Width)
xPos = 0
yPos = 0+ (FrameHeight - DestHeight) / 2
else
DestHeight = SourceImage.Height
DestWidth = SourceImage.Width
xPos = 0 + (FrameWidth - DestWidth) / 2
yPos = 0 + (FrameHeight - DestHeight) / 2
end if
end if
dim Resized as New Picture(FrameWidth, FrameHeight)
Resized.graphics.DrawPicture(SourceImage, xPos, yPos, DestWidth, DestHeight, 0, 0, SourceImage.Width, SourceImage.Height)
return Resized
[quote=329680:@Nicholas Henson]Hi i created a method that will scale pictures that users upload to be viewed in an ImageWell. However, i’m getting results that cause the image to not scale properly. Does anyone see anything i am doing wrong?
Code:
[code] dim landscape as Boolean
if CurrentPicture.Picture.Width > CurrentPicture.Picture.Height then
landscape = true
elseif CurrentPicture.Picture.Height > CurrentPicture.Picture.Width then
landscape = false
end if
dim scaleDown as Boolean
if CurrentPicture.Picture.Width > CurrentPicture.Width then
scaleDown = true
elseif CurrentPicture.Picture.Height > CurrentPicture.Height then
scaleDown = true
else
scaleDown = false
end if
dim ratio as double
if landscape then
if scaleDown then
ratio = CurrentPicture.Picture.Width / CurrentPicture.Width
ScaleWidth = CurrentPicture.Picture.Width / ratio
ScaleHeight = CurrentPicture.Picture.Height / ratio
else
ratio = CurrentPicture.Width / CurrentPicture.Picture.Width
ScaleWidth = CurrentPicture.Picture.Width * ratio
ScaleHeight = CurrentPicture.Picture.Height * ratio
end if
else
if scaleDown then
ratio = CurrentPicture.Picture.Height / CurrentPicture.Height
ScaleWidth = CurrentPicture.Picture.Width / ratio
ScaleHeight = CurrentPicture.Picture.Height / ratio
else
ratio = CurrentPicture.Height / CurrentPicture.Picture.Height
ScaleWidth = CurrentPicture.Picture.Width * ratio
ScaleHeight = CurrentPicture.Picture.Height * ratio
end if
end if[/code]
Results: Some Images display as expected.
link text
link text
link text
link text[/quote]
Nicholas, you’re repeating an awful lot of stuff there. Why not calculate the ratio once?
dim ratio = min(CurrentPicture.Picture.Width / CurrentPicture.Width, CurrentPicture.Picture.Height / CurrentPicture.Height)
ScaleWidth = CurrentPicture.Picture.Width * ratio
ScaleHeight = CurrentPicture.Picture.Height * ratio
[quote=329747:@Greg O’Lone]Nicholas, you’re repeating an awful lot of stuff there. Why not calculate the ratio once?
dim ratio = min(CurrentPicture.Picture.Width / CurrentPicture.Width, CurrentPicture.Picture.Height / CurrentPicture.Height)
ScaleWidth = CurrentPicture.Picture.Width * ratio
ScaleHeight = CurrentPicture.Picture.Height * ratio
[/quote]
Thanks Greg. I’m still a newbie after all these years. I did have to flip the division though. This is now the new code.
[code]ResizeImage(Image as Picture,TheControl as WebControl) as Picture
dim ratio as double = min(TheControl.Width / Image.Width, TheControl.Height / Image.Width)
ScaleWidth = Image.Width * ratio
ScaleHeight = Image.Height * ratio
Return ScalePicture(Image,ScaleWidth,ScaleHeight)[/code]