September 30th, 2003, 01:38 AM
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?
September 30th, 2003, 03:48 AM
go to properties windows
set the image size to "stretch"
September 30th, 2003, 01:10 PM
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
If Width > Height
Orientation = "H"
Orientation = "V"
If Orientation = "H" and Width > 800
resize (proportionally) based on 800 Width
If Orientation = "V" and Height > 600
resize (proportionally) based on 600 Height
Does this look like it will work?
How do I resize the pictures (for display)? Thumbnail view?
September 30th, 2003, 04:18 PM
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
September 30th, 2003, 05:00 PM
The StretchImage setting will reduce or enlarge the picture to fit the picture box control.
September 30th, 2003, 06:21 PM
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
September 30th, 2003, 11:02 PM
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.
October 1st, 2003, 09:22 AM
Try: PictureBox1.Image = Nothing
October 1st, 2003, 08:06 PM
April 11th, 2013, 06:35 AM
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...
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.