Results 1 to 12 of 12

Thread: [RESOLVED] Dynamically sizing an ImageBox based on image size

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Resolved [RESOLVED] Dynamically sizing an ImageBox based on image size

    I am having trouble getting this code to work the way I want. I think it might have something to with the .Stretch property of the ImageBox being set to true. But when I set it to false it doesn't act at ALL like I want it to.

    If anyone could help with this I would be very appreciative. The project is attached.
    Attached Files Attached Files
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Dynamically sizing an ImageBox based on image size

    Im looking your code.
    Do you want the image to have the same height than the PictureBox?
    Last edited by jcis; Nov 18th, 2005 at 02:39 AM.

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Dynamically sizing an ImageBox based on image size

    I want the image to be scaled based on the size of the picture box (which isn't a square). So the code first needs to decide which is dimension of the image is bigger than the picture box (the width or the height). Then it needs to scale that dimension based on the dimensions of the picture box.

    That should yeild a percent that we can use to scale both the percents.

    In short, I want the picture to be scaled into the picture box, but keep its proportions.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    Member
    Join Date
    Oct 2005
    Posts
    38

    Re: Dynamically sizing an ImageBox based on image size

    eyeRMonkey,
    First, thanks foryour useful links...

    I tried running your project. What I notice is if
    1) I run image 1 first, it does not resize to the picture box
    2) I restart program, run image 3, then image 1...image 1 appears to work the way you describe you want...
    3) I restart program, run image 2, then image 1...image 1 is paints beyond the border of the picture box

    Q: Am I right? Start Program, show Image 3, then image 1 and you get what you are looking for? I was unclear about what you were saying in your post.

    -MagicT
    MagicT

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Dynamically sizing an ImageBox based on image size

    Well, finally, I think I got it.

    VB Code:
    1. Private Sub SizePreview()
    2.     Dim lDif     As Double
    3.     Dim lPercent As Double
    4.  
    5.     With imgPreview
    6.         .Stretch = True
    7.         .Visible = False
    8.         If .Picture.Height = 0 Then Exit Sub
    9.  
    10.         If (.Picture.Height - picHolder.Height) > (.Picture.Width - picHolder.Width) Then
    11.             lDif = .Picture.Height - picHolder.Height
    12.             lPercent = lDif * 100 / .Picture.Height
    13.             .Width = .Picture.Width - (lPercent * .Picture.Width / 100)
    14.             .Height = .Picture.Height - (lPercent * .Picture.Height / 100)
    15.         Else
    16.             lDif = .Picture.Width - picHolder.Width
    17.             lPercent = lDif * 100 / .Picture.Width
    18.             .Width = .Picture.Width - (lPercent * .Picture.Width / 100)
    19.             .Height = .Picture.Height - (lPercent * .Picture.Height / 100)
    20.         End If
    21.  
    22.         .Move 0, 0, .Width, .Height
    23.         .Visible = True
    24.     End With
    25. End Sub
    Last edited by jcis; Nov 18th, 2005 at 05:33 AM. Reason: ;)

  6. #6

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Dynamically sizing an ImageBox based on image size

    Thanks a ton for trying, but that doesn't quite do what I need either. It doesn't maintain the aspect ratio of the original picture. If you make the PictureBox very very wide and go to picture 2 then it stretches to fit the box.

    What I want it to do figure out which dimension is biggest (I think your code does that). Then I want the image to be scaled so it fits inside the picture box without losing its original aspect ratio.

    It LOOKs like your code does what I want, but I think something is too dependent on the width/height of the PictureBox and that is causing it to stretch to the size of the picturebox.


    PS - The .Move at the end is unecessary because setting the .Width and .Height move it anyway. .Move is only for when you want to prevent flickering by changing everything at once using numbers ot variables, not the properties that already do that.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Dynamically sizing an ImageBox based on image size

    VB Code:
    1. Private Sub SizePreview()
    2.     Dim lDif     As Double
    3.     Dim lPercent As Double
    4.     Dim lx       As Long
    5.     Dim ly       As Long
    6.     Dim Xscaled  As Double
    7.     Dim Yscaled  As Double
    8.    
    9.     With imgPreview
    10.         .Visible = False
    11.         If .Picture.Height = 0 Then Exit Sub
    12.         .Stretch = False
    13.        
    14.         Xscaled = .Picture.Width * 0.5712
    15.         Yscaled = .Picture.Height * 0.5712
    16.        
    17.         If Yscaled > picHolder.Height Or Xscaled > picHolder.Width Then
    18.             If (Yscaled > picHolder.Height) Then
    19.                 lDif = Yscaled - picHolder.Height
    20.                 lPercent = lDif * 100 / Yscaled
    21.                 .Width = Xscaled - (lPercent * Xscaled / 100)
    22.                 .Height = Yscaled - (lPercent * Yscaled / 100)
    23.                 Xscaled = .Width  'Update Width
    24.                 Yscaled = .Height 'Update Height
    25.             End If
    26.             If (Xscaled > picHolder.Width) Then
    27.                 lDif = Xscaled - picHolder.Width
    28.                 lPercent = lDif * 100 / Xscaled
    29.                 .Width = Xscaled - (lPercent * Xscaled / 100)
    30.                 .Height = Yscaled - (lPercent * Yscaled / 100)
    31.             End If
    32.             .Stretch = True
    33.         End If
    34.                        
    35.         'this centers the image
    36.         lx = ly = 0
    37.         If .Width < picHolder.Width Then lx = (picHolder.Width - .Width) / 2
    38.         If .Height < picHolder.Height Then ly = (picHolder.Height - .Height) / 2
    39.         .Move lx, ly, .Width, .Height
    40.         .Visible = True
    41.     End With
    42. End Sub
    Last edited by jcis; Nov 18th, 2005 at 05:42 PM.

  8. #8

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Dynamically sizing an ImageBox based on image size

    I didn't mean the changing the picture boxes.stretch property. I just meant that it seems like the picture box is somethins stretching itself (getting wider) to the width of the picture box and that makese me think that the calculations are wrong.

    I haven't actually tested your newest code yet, but I will when I get home later today.

    Is there confusion on what I am asking for? I didn't feel that I explained it very well. Or maybe I am just getting different results on my computer than you are getting.

    Thanks again and I will let you know if that worked.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Dynamically sizing an ImageBox based on image size

    Ok, now i think its not working fine always.
    And i see picHolder.Height and ImgPreview.Picture.Height use different scales, something like picHolder.Height = ImgPreview .Picture.Width * 0.5712.

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Dynamically sizing an ImageBox based on image size

    Well, I edited my post number #7 and pasted a new function, that one is working perfectly here, i tested chaging the picturebox size many times, im testing with 15 different jpgs, now all of them show well.

  11. #11

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Dynamically sizing an ImageBox based on image size

    Wow! Thanks so much jcis. That is perfect. I wish I could rep you again. I will try to remember once the system lets me.

    Thanks a million.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  12. #12
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: [RESOLVED] Dynamically sizing an ImageBox based on image size

    You're welcome

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