Centering & resizing pics in a picture box
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Centering & resizing pics in a picture box

  1. #1
    Join Date
    Sep 2003
    Posts
    11

    Question Centering & resizing pics in a picture box

    I'm still a VB newbie. I can put an image into a picture box and I can get the properties of that image (width, height), but I cant' figure out how to center that image within the picturebox, or, in the case the picture is too big for the box, resize the image. It seems ike I'm missing something simple. Can someone help me?

  2. #2
    Join Date
    Sep 2003
    Posts
    30
    go to properties windows
    set the image size to "stretch"

  3. #3
    Join Date
    Sep 2003
    Posts
    11

    Lightbulb

    Thanks for the help. I don't know why I couldn't find it before I posted, but I found that property after I left the post. The center option is what I was looking for, but I still have an issue of sizes. Some pictures are too big to display in the picturebox. Here is what I came up with:
    I have a picturebox 800Wx600H
    I have an array of file names of pictures to display
    Dim Orientation as String
    for each:
    get Width
    get Height
    If Width > Height
    Orientation = "H"
    Else
    Orientation = "V"
    End If
    If Orientation = "H" and Width > 800
    resize (proportionally) based on 800 Width
    End if
    If Orientation = "V" and Height > 600
    resize (proportionally) based on 600 Height
    End If

    display picture

    Does this look like it will work?
    How do I resize the pictures (for display)? Thumbnail view?

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    This'll shrink the picture by half everytime the button is pressed until the image fits entirely in the picturebox:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim szImage As Size
    Dim szBox As Size
    szImage = PictureBox1.Image.Size()
    szBox = PictureBox1.Size
    If szImage.Width > szBox.Width Or szImage.Height > szBox.Height Then
    ' Get the scale factor.
    Dim scale_factor As Single = 0.5

    ' Get the source bitmap.
    Dim bm_source As New Bitmap(PictureBox1.Image)

    ' Make a bitmap for the result.
    Dim bm_dest As New Bitmap( _
    CInt(bm_source.Width * scale_factor), _
    CInt(bm_source.Height * scale_factor))

    ' Make a Graphics object for the result Bitmap.
    Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

    ' Copy the source image into the destination bitmap.
    gr_dest.DrawImage(bm_source, 0, 0, _
    bm_dest.Width + 1, _
    bm_dest.Height + 1)

    ' Display the result.
    PictureBox1.Image = bm_dest

    End If

    End Sub

  5. #5
    Join Date
    Sep 2003
    Location
    MO
    Posts
    15
    The StretchImage setting will reduce or enlarge the picture to fit the picture box control.

  6. #6
    Join Date
    Sep 2003
    Posts
    11

    Talking

    Thank you for the help. I can use some of that to modify my code. I'm anxious to try it out. Also, I don't want to use the stretch feature, because it distorts the image and the images are all different sizes

  7. #7
    Join Date
    Sep 2003
    Posts
    11

    Thumbs up

    It worked very well. Thank you DSJ. Now if I can only clear the picturebox... I figured Image.dispose would do it, but it doesn't and I don't see another option.

  8. #8
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Try: PictureBox1.Image = Nothing

  9. #9
    Join Date
    Sep 2003
    Posts
    11

    Talking

    Once again, thank you.

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: Centering & resizing pics in a picture box

    <sarcastic>wow! finally after 10 years this thread has received an answer!</sarcastic>

    Please do not revive old threads!

    There are many active threads that need help...

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center