10

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

3 Answers3

11

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.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • Yes I have done that and worked but I need to show them with it's actual width and height and at the same time I need to scale the image so that it should fit in that as I have given an example which resembles lightbox with a link and I need to achieve that. –  Aug 11 '11 at 18:10
  • 1
    @volety: in your question you state that you need to scale them. You can't scale and have them in their actual size at the same time. – Fredrik Mörk Aug 11 '11 at 18:12
  • I need a simple animation to the picturebox so that it should expand to the size of the Image so how do I do that? –  Aug 11 '11 at 18:16
  • Ah, now I understand. That is a completely different story. That will probably be a lot easier to achieve in WPF. – Fredrik Mörk Aug 11 '11 at 18:17
  • Thanks Fredik for helping me and can you please provide me any link for me and Can I convert my VB.net project to WPF? –  Aug 11 '11 at 18:19
  • @volety: sorry, never did that so I don't really know any tools for it – Fredrik Mörk Aug 11 '11 at 18:34
  • No problem I'm in search of that anyways thanks for making me to think in the other way. –  Aug 11 '11 at 18:37
3

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

2

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 :)

Kym NT
  • 670
  • 9
  • 28