Solved

.Net PictureBox control / Image class scaling

Posted on 2004-04-29
1
2,309 Views
Last Modified: 2013-11-22
Hi all,

I'm doing a small picture viewer and I'd like to scale the pictures so they fit in a PictureBox control (VB.Net).  The SizeMode property doesn't do what I want, because there is no option to scale the image while maintaining the aspect ratio.  Help on this would be greatly appreciated.  Thanks!
0
Comment
Question by:instruo
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 110 total points
ID: 10954728
Here is one way to do it.  Run the app, select an image and try resizing the form.

Idle_Mind

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents imageSelectButton As System.Windows.Forms.Button
    Friend WithEvents imageName As System.Windows.Forms.Label
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.imageSelectButton = New System.Windows.Forms.Button
        Me.imageName = New System.Windows.Forms.Label
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'imageSelectButton
        '
        Me.imageSelectButton.Location = New System.Drawing.Point(8, 16)
        Me.imageSelectButton.Name = "imageSelectButton"
        Me.imageSelectButton.Size = New System.Drawing.Size(80, 24)
        Me.imageSelectButton.TabIndex = 0
        Me.imageSelectButton.Text = "Select Image"
        '
        'imageName
        '
        Me.imageName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.imageName.Location = New System.Drawing.Point(96, 8)
        Me.imageName.Name = "imageName"
        Me.imageName.Size = New System.Drawing.Size(380, 40)
        Me.imageName.TabIndex = 1
        Me.imageName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'PictureBox1
        '
        Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.PictureBox1.Location = New System.Drawing.Point(8, 48)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(468, 368)
        Me.PictureBox1.TabIndex = 2
        Me.PictureBox1.TabStop = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(480, 422)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.imageName)
        Me.Controls.Add(Me.imageSelectButton)
        Me.Name = "Form1"
        Me.Text = "One way to Scale an Image while maintaining its Aspect Ratio"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub imageSelectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imageSelectButton.Click
        OpenFileDialog1.Title = "Please select an Image to view..."
        OpenFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            imageName.Text = OpenFileDialog1.FileName
            Form1_Resize(Nothing, Nothing)
        End If
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If imageName.Text <> "" Then
            Dim img As Image
            Dim ratio As Double
            Dim s As Size

            If PictureBox1.Width > 0 And PictureBox1.Height > 0 Then
                Try
                    img = Image.FromFile(imageName.Text)
                    ratio = img.Height / img.Width
                    s.Width = PictureBox1.Width
                    s.Height = s.Width * ratio
                    If s.Height > PictureBox1.Height Then
                        ratio = PictureBox1.Height / s.Height
                        s.Width = s.Width * ratio
                        s.Height = s.Height * ratio
                    End If
                    If s.Width > 0 And s.Height > 0 Then
                        PictureBox1.Image = New Bitmap(img, s)
                    End If
                    img.Dispose()
                Catch ex As Exception
                    MsgBox("Unable to Load Image: " & vbCrLf & imageName.Text, MsgBoxStyle.Critical, "Error Loading Image File")
                End Try
            End If
        End If
    End Sub
End Class
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question