Results 1 to 19 of 19

Thread: [RESOLVED] Looking for Favor (image resize)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Resolved [RESOLVED] Looking for Favor (image resize)

    I am looking for a favor here... I am going to sleep soon, and I just can not get this to work....

    Can someone write code which will work will work with the following dimensions:

    [/quote]Form Height: 15 000
    Form Width: 13 000

    picture1.height = 12 000
    picture1.width = 13 000

    1 label with the percentage that it is shrunk or grew.[/quote]

    All I want is a picture load into the picture1 and resized accordingly (and give me the percentage of the resize in the label)... Also, I do not want it stretched too much on one side...

    If someone could give me this code I would be grateful...
    Last edited by Zeratulsdomain; Oct 31st, 2005 at 04:01 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: Looking for Favor (image resize)

    try doing it this way, coding it is something u'll have to do u're self
    get the aspect ratio of the image (x:y)
    decrease the width of the image to what u want
    now decrease the height of the image accrding to the aspect ratio

    as for the percentage, %=(newX/oldX)*100

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Looking for Favor (image resize)

    Try this:
    VB Code:
    1. Public Sub LoadPic(ByVal sFileName As String)
    2.     Dim pic As StdPicture
    3.     Dim nWidth As Long
    4.     Dim nHeight As Long
    5.     Dim nWidthOrig As Long
    6.     Dim nHeightOrig As Long
    7.     Dim hDiff As Double, wDiff As Double, nDiff As Double
    8.    
    9.     Set pic = LoadPicture(sFileName)
    10.     With Picture1
    11.         .AutoRedraw = True
    12.         nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
    13.         nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
    14.         'The picture box width is assumed to be larger then the height
    15.         If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
    16.             wDiff = nWidthOrig / .ScaleWidth
    17.             hDiff = nHeightOrig / .ScaleHeight
    18.             nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
    19.         End If
    20.         If nDiff Then
    21.             nWidth = nWidthOrig / nDiff
    22.             nHeight = nHeightOrig / nDiff
    23.             Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
    24.         End If
    25.         .Cls
    26.         .PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
    27.          (.ScaleHeight - nHeight) \ 2, _
    28.          nWidth, nHeight, , , , , vbSrcCopy
    29.     End With
    30. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Re: Looking for Favor (image resize)

    VB Code:
    1. Public Sub LoadPic(ByVal sFileName As String)
    2.     Dim pic As StdPicture
    3.     Dim nWidth As Long
    4.     Dim nHeight As Long
    5.     Dim nWidthOrig As Long
    6.     Dim nHeightOrig As Long
    7.     Dim hDiff As Double, wDiff As Double, nDiff As Double
    8.         Set pic = LoadPicture(sFileName)
    9.     With Picture1
    10.         .AutoRedraw = True
    11.         nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
    12.         nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
    13.         'The picture box width is assumed to be larger then the height
    14.         If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
    15.             wDiff = nWidthOrig / .ScaleWidth
    16.             hDiff = nHeightOrig / .ScaleHeight
    17.             nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
    18.         End If
    19.         If nDiff Then
    20.             nWidth = nWidthOrig / nDiff
    21.             nHeight = nHeightOrig / nDiff
    22.             Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
    23.         End If
    24.         .Cls
    25.         .PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
    26.          (.ScaleHeight - nHeight) \ 2, _
    27.          nWidth, nHeight, , , , , vbSrcCopy
    28.     End With
    29. End Sub
    30.  
    31. Private Sub Command1_Click()
    32. LoadPic ("C:\Documents and Settings\Chris\Desktop\IMG\sky13.jpg")
    33. End Sub

    When I click on the image I get a invalid procedure or call argument.

    The following code is in big bright yellow:
    VB Code:
    1. .PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
    2.          (.ScaleHeight - nHeight) \ 2, _
    3.          nWidth, nHeight, , , , , vbSrcCopy

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Looking for Favor (image resize)

    Is the name of your control Picture1? You shoud use Option Explicit to help you find errors.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Angry Re: Looking for Favor (image resize)

    Yes I have a picture1 and a label1

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Looking for Favor (image resize)

    Where did you paste the code? It should reside in the same Form as you have the Picture1 picturebox in.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Re: Looking for Favor (image resize)

    I only have one form.... I have a Picture1, Label1 and a Command1

    Command1 has:
    VB Code:
    1. LoadPic ("C:\Documents and Settings\Chris\Desktop\IMG\sky13.jpg")

    The rest of the code was just copy/pasted

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Looking for Favor (image resize)

    And you are using VB6?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Re: Looking for Favor (image resize)

    yes...6.0.8169

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Looking for Favor (image resize)

    I just put a picturebox, a label and a command button on a Form. In the click event of the command button I call the above procedure and it works just fine.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Re: Looking for Favor (image resize)

    Can you upload the project then?

    Right from the very beginning I had a bad felling this image thing would screw me in at every corner (hence why I quickly gave up on it after 1-2 hours and posted this tread)...

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Looking for Favor (image resize)

    OK, here is my test project.
    Attached Files Attached Files

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Re: Looking for Favor (image resize)

    Quote Originally Posted by Joacim Andersson
    OK, here is my test project.

    That doesn't give me a error... But it doesnt work when I make the window bigger (I use 2048 by 1536)

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Looking for Favor (image resize)

    Sorry, but I don't understand when you say "it doesn't work when I make the window bigger"??? Do you mean that if you maximize the window and then it doesn't work???

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Re: Looking for Favor (image resize)

    Sry, I was wrong... The bug with that code is if the image is smaller then the picture box it will not be displayed.

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Looking for Favor (image resize)

    Oh yeah... Try this:
    VB Code:
    1. Public Sub LoadPic(ByVal sFileName As String)
    2.     Dim pic As StdPicture
    3.     Dim nWidth As Long
    4.     Dim nHeight As Long
    5.     Dim nWidthOrig As Long
    6.     Dim nHeightOrig As Long
    7.     Dim hDiff As Double, wDiff As Double, nDiff As Double
    8.         Set pic = LoadPicture(sFileName)
    9.     With Picture1
    10.         .AutoRedraw = True
    11.         nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
    12.         nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
    13.         nWidth = nWidthOrig
    14.         nHeight = nHeightOrig
    15.         'The picture box width is assumed to be larger then the height
    16.         If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
    17.             wDiff = nWidthOrig / .ScaleWidth
    18.             hDiff = nHeightOrig / .ScaleHeight
    19.             nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
    20.         End If
    21.         If nDiff Then
    22.             nWidth = nWidthOrig / nDiff
    23.             nHeight = nHeightOrig / nDiff
    24.             Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
    25.         End If
    26.         .Cls
    27.         .PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
    28.          (.ScaleHeight - nHeight) \ 2, _
    29.          nWidth, nHeight, , , , , vbSrcCopy
    30.     End With
    31. End Sub

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    431

    Re: Looking for Favor (image resize)

    Tnx man (takes Advil)...

    Hopefully Ill finish my application tonight.

  19. #19
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    277

    Re: Looking for Favor (image resize)

    Quote Originally Posted by Joacim Andersson
    Oh yeah... Try this:
    VB Code:
    1. Public Sub LoadPic(ByVal sFileName As String)
    2.     Dim pic As StdPicture
    3.     Dim nWidth As Long
    4.     Dim nHeight As Long
    5.     Dim nWidthOrig As Long
    6.     Dim nHeightOrig As Long
    7.     Dim hDiff As Double, wDiff As Double, nDiff As Double
    8.         Set pic = LoadPicture(sFileName)
    9.     With Picture1
    10.         .AutoRedraw = True
    11.         nWidthOrig = .ScaleX(pic.Width, vbHimetric, .ScaleMode)
    12.         nHeightOrig = .ScaleY(pic.Height, vbHimetric, .ScaleMode)
    13.         nWidth = nWidthOrig
    14.         nHeight = nHeightOrig
    15.         'The picture box width is assumed to be larger then the height
    16.         If nWidthOrig > .ScaleWidth Or nHeightOrig > .ScaleHeight Then
    17.             wDiff = nWidthOrig / .ScaleWidth
    18.             hDiff = nHeightOrig / .ScaleHeight
    19.             nDiff = IIf(wDiff > hDiff, wDiff, hDiff)
    20.         End If
    21.         If nDiff Then
    22.             nWidth = nWidthOrig / nDiff
    23.             nHeight = nHeightOrig / nDiff
    24.             Label1.Caption = "Ratio: " & (nWidth / nWidthOrig) * 100
    25.         End If
    26.         .Cls
    27.         .PaintPicture pic, (.ScaleWidth - nWidth) \ 2, _
    28.          (.ScaleHeight - nHeight) \ 2, _
    29.          nWidth, nHeight, , , , , vbSrcCopy
    30.     End With
    31. End Sub
    hi joacim,

    your code works well. i currently need help from the similar senario. your code work perfectly when i insert a huge file and it load beatifully in to teh picturebox .how can i rescale a smaller image to fit the picturebox(stretching of smaller image to fit a bigger picture box?

    ???
    ocw

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width