Question Resizing my image

AnthonyRicketts

New member
Joined
Nov 29, 2008
Messages
1
Programming Experience
1-3
Hello All,

I am resizing an image uploaded by the user to two different sizes, then saving the resized images on the server, and storing the filename in the database. It all works fine except when the image is resized, if it is not a perfect square, it leaves blank space on either size of the image.

For example if the image is taller than wider, there will be space on the right and left of the image. If the image is wider than taller, there will be space above and below the image.

How might I correct this? My code is listed below. Thank you for any help.



VB.NET:
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If fileImage.HasFile Then
            Dim filepath As String = "./images/" & userID & "/" & fileImage.FileName
            fileImage.SaveAs(Server.MapPath(filepath))
        End If

        'Resize Image
        Dim FileToResize As String = Server.MapPath("./images/" & userID & "/" & fileImage.FileName)
        Using originalBitmap As Bitmap = Bitmap.FromFile(FileToResize, True), newbmp As Bitmap = New Bitmap(200, 200)
            Dim WidthVsHeightRatio = CDec(originalBitmap.Width) / CDec(originalBitmap.Height)

            Using newg As Graphics = Graphics.FromImage(newbmp)
                newg.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                newg.Clear(Color.White)

                If WidthVsHeightRatio = 1D Then
                    newg.DrawImage(originalBitmap, 50, 50, 200, 200)
                    newg.Save()

                ElseIf WidthVsHeightRatio < 1D Then 'Image is taller than wider

                    newg.DrawImage(originalBitmap, New RectangleF(New PointF((100 - (200 * WidthVsHeightRatio) / 2), 0), New SizeF(200 * WidthVsHeightRatio, 200.0F)))
                    newg.Save()

                Else 'Image is wider than taller

                    Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1)
                    newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, 100 - ((200 * inverse) / 2)), New SizeF(200.0F, 200 * inverse)))
                    newg.Save()
                End If
            End Using

            newbmp.Save(Server.MapPath("./images/" & userID & "/tn_" & fileImage.FileName), System.Drawing.Imaging.ImageFormat.Jpeg)
        End Using

        'Resize Image 2
                Dim FileToResize As String = Server.MapPath("./images/" & userID & "/" & fileImage.FileName)
        Using originalBitmap As Bitmap = Bitmap.FromFile(FileToResize, True), newbmp As Bitmap = New Bitmap(100, 100)

            Dim WidthVsHeightRatio = CDec(originalBitmap.Width) / CDec(originalBitmap.Height)
            Using newg As Graphics = Graphics.FromImage(newbmp)
                newg.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

                newg.Clear(Color.White)

                If WidthVsHeightRatio = 1D Then

                    newg.DrawImage(originalBitmap, 0, 0, 100, 100)

                    newg.Save()

                ElseIf WidthVsHeightRatio < 1D Then 'Image is taller than wider

                    newg.DrawImage(originalBitmap, New RectangleF(New PointF((50 - (100 * WidthVsHeightRatio) / 2), 0), New SizeF(100 * WidthVsHeightRatio, 100.0F)))
                    newg.Save()

                Else 'Image is wider than taller

                    Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1)
                    newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, 50 - ((100 * inverse) / 2)), New SizeF(100.0F, 100 * inverse)))
                    newg.Save()

                End If

            End Using
            newbmp.Save(Server.MapPath("./images/" & userID & "/tn2_" & fileImage.FileName), System.Drawing.Imaging.ImageFormat.Jpeg)
        End Using
        imageDisplay.ImageUrl = "./images/" & userID & "/tn_" & fileImage.FileName
        imageDisplay.Visible = True
        lblCaptionDisplay.Visible = True
        lblCaptionDisplay.Text = txtCaption.Text
        txtImageTN.Text = "tn_" & fileImage.FileName
        txtImageTN2.Text = "tn2_" & fileImage.FileName
        txtImage.Text = fileImage.FileName

    End Sub
 

Latest posts

Back
Top