Rescaling all images to be same size

Posted on 2011-10-19
Last Modified: 2012-06-21
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
                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
Question by:Overthere
    LVL 25

    Accepted Solution

    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

        '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.

    Author Comment

    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 please stay tuned!

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now