• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Rescaling all images to be same size

I have images in my images dorectory that when I display them, I want them to all be the same size, in other words I need to rescale them but it isn't working! (imagine that)
I dynamically display in a datalist and whatever the size of the picture - that's how it display!
What am I doing wrong? I know I may lose resolution but all of the images need to be the same size when I dispaly them..
any help appericated...



Sub ScaleThem(ByRef imgHeight As Integer, ByRef imgWidth As Integer, currentImage as System.Drawing.Image)
       
        Dim maxWidth As Integer = 125
        Dim maxHeight As Integer = 93
        'Response.Write("at Scale..height.." & imgHeight & "At scale..width.." & imgWidth & "</br>")
     
       
        Dim scaleFactor As Double
       
        If imgWidth > maxWidth Or imgHeight > maxHeight Then
            If (maxHeight / imgHeight) > (maxWidth / imgWidth) Then
                scaleFactor = maxHeight / imgHeight
            Else
                scaleFactor = maxWidth / imgWidth
            End If
        End If
   
        If imgWidth > maxWidth Then
            scaleFactor = maxWidth / imgWidth
            imgWidth *= scaleFactor
            imgHeight *= scaleFactor
        End If
   
        If imgHeight > maxHeight Then
            imgWidth /= scaleFactor
            imgHeight /= scaleFactor
            scaleFactor = maxHeight / imgHeight
            imgWidth *= scaleFactor
            imgHeight *= scaleFactor
        End If
    End Sub
0
Overthere
Asked:
Overthere
1 Solution
 
Luis PérezSoftware Architect in .NetCommented:
The problem here is that you're in fact doing nothing except for size calculations. But you're not applying those calculations to effectively resize the image.

Try this:

Public Function ScaleImage(ByVal original As System.Drawing.Bitmap, ByVal maxWidth As Integer, ByVal maxHeight As Integer) As System.Drawing.Bitmap
    'Calculate the output image width and height based on aspect ratio
    Dim ratio As Single = Math.Min(maxWidth / original.Width, maxHeight / original.Height)
    Dim newWidth As Integer = ratio * original.Width
    Dim newHeight As Integer = ratio * original.Height

    'Create the new image with calculated dimensions
    Dim output As System.Drawing.Bitmap = New System.Drawing.Bitmap(newWidth, newHeight)
    Dim g As Graphics = Graphics.FromImage(output)

    'Apply hi-quality properties to the new image
    With g
        .CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
        .InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
        .PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
    End With

    'Draw the original image into the new image (with the appropieate scale)
    g.DrawImage(original, 0, 0, newWidth, newHeight)

    'Dispose the graphics object
    g.Dispose()

    'Return the output image
    Return output
End Function

Calling this function passing a Bitmap and a maximum width and height will produce a hi-quality output image without distortion that will not exceed the maximun size dimensions.

Hope that helps.
0
 
OverthereAuthor Commented:
Thank you - I feel pretty silly (I am returning to programming after a 4 yr absent) and your coding works well but....
still have another problem and I will post that seperately ....so please stay tuned!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now