Results 1 to 5 of 5

Thread: [RESOLVED] Picture Box and aspect ratio?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Resolved [RESOLVED] Picture Box and aspect ratio?

    Is there an easy way to show a picture in a picture box and that picture to look normal? I can't find a way to resize it without being grotesquely streched or filling 10 monitors (when letting the control to resize itself around).
    I guess I could calculate the width and height and resize the image in advance but hopefully there is something like an actual working sane resize?
    VB 2005, Win Xp Pro sp2

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Picture Box and aspect ratio?

    That can be done with a moments thought and a few lines of code. Set the SizeMode of the PictureBox to StretchImage and then load the image like this:
    VB Code:
    1. Dim img As Image = Image.FromFile("E:\Documents\John\My Pictures\DataGridView.JPG")
    2. Dim imageSize As Size = img.Size
    3. Dim aspectRatio As Double = imageSize.Height / imageSize.Width
    4.  
    5. Me.PictureBox1.Width = Convert.ToInt32(Math.Round(Me.PictureBox1.Height / aspectRatio))
    6. Me.PictureBox1.Image = img
    To not know is no crime but can I suggest that you don't come in blasting the tools or the technology for not working exactly as you want when what you want is not necessarily the only or best way to do something and achieving your aim is simple anyway. Expecting the control AND image to resize simultaneously is not necessarily logical. If that's what you want then you can make it happen easily as shown, but to have it happen of its own accord is not sensible because it would require that you set properties to maintain the aspect ratio and whether both or just one dimension should remain constant. There'd be just as much work involved doing it that way as the way I've shown. Just because you want something doesn't mean it should be the default.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Picture Box and aspect ratio?


    It is not about resizing both, it is about being able to fit an image without distorting it and without changing the allocated area. This includes black(white etc) stripes / inactive fields on chosen sides. It is like a TV set, they don't jump horizontally or vertically in order to accomodate the picture, they resize that picture. Not many applications lack that autofit feature and for a reason. It is way more practical than showing only the dead center of a 2816x2112 image.

    I guess I should look for a way to resize the picture accordingly in advance and then give it to the control. I was hoping there is already a solution as mine wouldn't be really time saving.
    VB 2005, Win Xp Pro sp2

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Picture Box and aspect ratio?

    I guess I misunderstood your first post. What you are asking for has been provided in .NET 2.0 with the addition of Zoom to the PictureBoxSizeMode enumeration. In .NET 1.x you are stuck with creating a new Bitmap object of the appropriate dimensions first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: Picture Box and aspect ratio?

    Try this
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim lImage As Image = Image.FromFile("E:\Graphics_Moth.jpg") 'TO DO: Add the Path to your Image file here
    3.         ResizePicture(Me.PictureBox1, lImage)
    4.     End Sub
    5.  
    6.     Private Sub ResizePicture(ByVal pBox As PictureBox, ByVal pImage As Image)
    7.         Dim lnewWidth As Integer, lnewHeight As Integer
    8.         Dim lWidth As Integer = pImage.Width
    9.         Dim lHeight As Integer = pImage.Height
    10.  
    11.         'If image Width > pictureBox Width, resize Width
    12.         If lWidth > pBox.Width Then
    13.             lnewWidth = pBox.Width                   'New Width = PBox Width
    14.             lHeight = Convert.ToInt32(lHeight * (lnewWidth / lWidth)) 'Risize Height keeping proportions
    15.         Else
    16.             lnewWidth = lWidth                       'If not, keep the original Width value
    17.         End If
    18.  
    19.         'If the image Height > The pictureBox Height, resize Height
    20.         If lHeight > pBox.Height Then
    21.             lnewHeight = pBox.Height              'New Height = PB Height
    22.             lnewWidth = Convert.ToInt32(lnewWidth * (lnewHeight / lHeight))  'Risize Width keeping proportions
    23.         Else
    24.             lnewHeight = lHeight                            'If not, use the same value
    25.         End If
    26.  
    27.         Dim lBitmap As Bitmap = New System.Drawing.Bitmap(lnewWidth, lnewHeight)
    28.         Dim lGr As Graphics = System.Drawing.Graphics.FromImage(lBitmap)
    29.  
    30.         'Add resized to Picturebox, if you want it centered: In Design mode, Change SizeMode property to CenterImage
    31.         lGr.DrawImage(pImage, 0, 0, lnewWidth, lnewHeight)
    32.         pBox.Image = lBitmap
    33.  
    34.         lGr.Dispose()
    35.         lGr = Nothing
    36.     End Sub
    If you want it centered change the PictureBox SizeMode property to CenterImage.
    Last edited by jcis; Nov 6th, 2006 at 02:43 AM.

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