|
-
Nov 5th, 2006, 10:59 PM
#1
Thread Starter
Frenzied Member
[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?
-
Nov 5th, 2006, 11:51 PM
#2
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:
Dim img As Image = Image.FromFile("E:\Documents\John\My Pictures\DataGridView.JPG")
Dim imageSize As Size = img.Size
Dim aspectRatio As Double = imageSize.Height / imageSize.Width
Me.PictureBox1.Width = Convert.ToInt32(Math.Round(Me.PictureBox1.Height / aspectRatio))
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.
-
Nov 6th, 2006, 01:16 AM
#3
Thread Starter
Frenzied Member
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.
-
Nov 6th, 2006, 01:50 AM
#4
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.
-
Nov 6th, 2006, 02:37 AM
#5
Re: Picture Box and aspect ratio?
Try this
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lImage As Image = Image.FromFile("E:\Graphics_Moth.jpg") 'TO DO: Add the Path to your Image file here
ResizePicture(Me.PictureBox1, lImage)
End Sub
Private Sub ResizePicture(ByVal pBox As PictureBox, ByVal pImage As Image)
Dim lnewWidth As Integer, lnewHeight As Integer
Dim lWidth As Integer = pImage.Width
Dim lHeight As Integer = pImage.Height
'If image Width > pictureBox Width, resize Width
If lWidth > pBox.Width Then
lnewWidth = pBox.Width 'New Width = PBox Width
lHeight = Convert.ToInt32(lHeight * (lnewWidth / lWidth)) 'Risize Height keeping proportions
Else
lnewWidth = lWidth 'If not, keep the original Width value
End If
'If the image Height > The pictureBox Height, resize Height
If lHeight > pBox.Height Then
lnewHeight = pBox.Height 'New Height = PB Height
lnewWidth = Convert.ToInt32(lnewWidth * (lnewHeight / lHeight)) 'Risize Width keeping proportions
Else
lnewHeight = lHeight 'If not, use the same value
End If
Dim lBitmap As Bitmap = New System.Drawing.Bitmap(lnewWidth, lnewHeight)
Dim lGr As Graphics = System.Drawing.Graphics.FromImage(lBitmap)
'Add resized to Picturebox, if you want it centered: In Design mode, Change SizeMode property to CenterImage
lGr.DrawImage(pImage, 0, 0, lnewWidth, lnewHeight)
pBox.Image = lBitmap
lGr.Dispose()
lGr = Nothing
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|