Proportional scaling

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
I'm sure this must be a maths thing, if it is I stuffed as I am naff at math!

I have a picture box sized at 960 * 536 pixels and I am using the code below to put images of different sizes into it. Some look OK others are distorted.

Code:
m_bmpNewImage = New Bitmap(m_bmpNewImage, 960, 536)

I have an image I am looking at now and it is distorted. Its dimensions are 2142*2856.

Is there some magical formula needed to scale it to look right? Any help gratefully received!

Thnx
 
can you set the
PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage
this will allow the picture to size according to the size of the picturebox

or
PictureBox2.SizeMode = PictureBoxSizeMode.Autoimage
this will size the picturebox to tyhe picture, so if the picture is much bigger it could take up the whole form
 
Unfortunately these do not fit the bill :(

The stretch image just distorts the image and as you say the autosize just makes it too big to fit in the form :(

I am trying to have it so the image is scaled correctly to fit into the picture box.
 
I think you will always have some distortion. You have a picture where your length is shorter than the width and putting that into the picturebox which your length is longer than your width. So you will always have a distorted look.

But if you don't mind space in the box. here is a formula. You need a ratio formula

536/2856 = L/2142 ------- L = 2142(536/2856)

SInce your width is the longest on the picture, You know you can make it the same width as the picturebox and need the length. Now if you have a picture that is the width that is shorter then you need to find the scale for the width

say your picture is 2500x2000 with youre picbox dimensions

w/2000 = 960/2500 ---------- w =2000(960/2500)

give this a go and hopefully is works
 
:confused:

I get an even more distorted image but now have a blank area at the bottom of the picture box?

The sizes used in this code are as follows

Image = length 2142, width 2856

Picturebox = length 536, width 960

Code:
            intWidth = m_bmpNewImage.Width

            intHeight = m_bmpNewImage.Height

            If intWidth > intHeight Then

                intHeight = intHeight * (intPictureBoxHeight / intWidth)

            Else

                intWidth = intHeight * (intPictureBoxWidth / intWidth)

            End If

            ' resize it to fit the picture box

            m_bmpNewImage = New Bitmap(m_bmpNewImage, intWidth, intHeight)

confused as hell :(
 
The idea was to try to make this a generic program that I could realise as a freebie on my website, but this looks like that won't happen.

I realsie now that my camera will always take the same size images, (I never change the resolution), so the image length will always be 70% of the width in normal view or the width will always be 70% of the height when taking picture with camera on its side.

Therefore I have created to picture boxes with this ration and it works :)

Still would like to now how to go about making it generic tho :confused:
 
Hi Hog,

You can re-size the images without distoration. Here's a resizing function that should work ok for you.

To use it check if your loading bitmap is larger in either width or height than the picture box.

Visual Basic:
    Private Function ResizeBitmap(ByVal bmpTooBig As Bitmap, ByVal maxWidth As Integer, ByVal maxHeight As Integer) As Bitmap
        Dim ratio, ratioW, ratioH As Decimal

        If bmpTooBig.Width > maxWidth Then
            ratioW = maxWidth / bmpTooBig.Width
        Else : ratioW = 1
        End If
        If bmpTooBig.Height > maxHeight Then
            ratioH = maxHeight / bmpTooBig.Height
        Else : ratioH = 1
        End If
        'determine which dimension is the smallest
        If ratioW <= ratioH Then
            ratio = ratioW
        Else : ratio = ratioH
        End If
        Dim bmpResized As New Bitmap(CInt(bmpTooBig.Width * ratio), CInt(bmpTooBig.Height * ratio))
        'Make a Graphics object for the resized bitmap.
        Dim g As Graphics = Graphics.FromImage(bmpResized)
        g.DrawImage(bmpTooBig, 0, 0, bmpResized.Width + 1, bmpResized.Height + 1)
        Return bmpResized

        'dispose of graphic objects
        g.Dispose()
    End Function

I hope that helps
 
Back
Top