user676589
user676589

Reputation:

Larger Image to fit in picturebox

As you'll can see the first image is the size of (1024*768) and it is correctly displayed in the picturebox and in the second case the image size is (1600*900) and it is displayed to half of the picturebox and the remaining is missing .So No I would like to fir that image in the picturebox no matter what the size is and even though it is greater than the size of the picturebox.I need to scale that Image.So how do I do that?And one more thing is that I need to resize the picturebox automatically when the image loads to it just as we see in the lightbox effect.. http://www.lokeshdhakar.com/projects/lightbox2/ -------->example.

Any help will be appreciated!

Here is what I am getting.

enter image description here

enter image description here

Upvotes: 10

Views: 60181

Answers (3)

Kym NT
Kym NT

Reputation: 790

The two Easiest Ways to Fit an Image to Any Size of PictureBox is:

-to set the Image as Background Image OR -to set it as picturebox image then set sizemode to StretchImage

1.Background Image

-use the BackgroundImage property of the PictureBox

        picturebox.BackgroundImage = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))

-Then Set its BackgroundImageLayout to stretch Like This:

        picturebox.BackgroundImageLayout = ImageLayout.Stretch
  1. Image -use the Image property of the PictureBox

    picturebox.Image = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
    

-Then Set its' sizeMode to StretchImage Like This:

    picturebox.SizeMode = PictureBoxSizeMode.StretchImage

This will make any Picture / Image / Canvas Stroke (converted to Byte Array) fit according to the height and width of the picturebox

Hope This Helps :)

Upvotes: 2

user1932998
user1932998

Reputation:

I know this is marked answered, but I wrote this for one of my own apps. Hope it helps somebody..

Private Sub ScaleImage(ByVal p As PictureBox, ByRef i As Bitmap)
    If i.Height > p.Height Then
        Dim diff As Integer = i.Height - p.Height
        Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
        i = Resized
End If
    If i.Width > p.Width Then
        Dim diff As Integer = i.Width - p.Width
        Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
        i = Resized
End If

End Sub

Upvotes: 3

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

If it's a winforms app, you can set the SizeMode property of the PictureBox to PictureBoxSizeMode.Zoom. Note that this will increase the size of smaller images to fill the frame, so you might want to measure the image first, in order to check if either edge is too long, and then setting SizeMode to either PictureBoxSizeMode.Zoom or PictureBoxSizeMode.Normal.

Upvotes: 12

Related Questions